2010-12-06 117 views
1

我必須在Android中進行拖動導航。我添加了拖動屏幕導航的代碼。這不像Android默認那樣流暢。下面的代碼給出了從左到右和從右到左的拖動導航,但問題是當你點擊右側然後到左側時,屏幕也會導航。什麼是實現它的正確途徑。我是否需要使用相同的代碼來計算X值?
下面的代碼。Android - 拖動屏幕導航

// On Drag Navigation 
public boolean onTouch(View arg0, MotionEvent arg1) { 

    // Get the action that was done on this touch event 
    switch (arg1.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
     { 
      // store the X value when the user's finger was pressed down 
      downXValue = arg1.getX(); 
      break; 
     } 

     case MotionEvent.ACTION_UP: 
     { 
      // Get the X value when the user released his/her finger 
      float currentX = arg1.getX();    

      // Going Backward: pushing stuff to the right 
      if (downXValue < currentX) 
      { 
       Intent lIntent = new Intent(getApplicationContext(), previousScreen.class); 
       startActivity(lIntent); 
       finish(); 
      } 

      // Going Forward: pushing stuff to the left 
      if (downXValue > currentX) 
      { 
       Intent lIntent = new Intent(getApplicationContext(), nextScreen.class); 
       startActivity(lIntent); 
       finish(); 
      } 
      break; 
     } 
    } 
} 

請指定實現它的正確方法。

在此先感謝

回答

0

你可能不希望使用單獨的活動這一點。此模式用於在同一活動內導航。

有兩個或三個州要考慮取決於你如何選擇考慮它。第一種是當用戶在屏幕上有手指並來回平移時的「拖動」狀態。第二和第三是用戶放手時發生的事情。如果放開時的速度低於某個閾值,(系統使用ViewConfiguration中的最小速度)爲最接近的屏幕添加動畫。如果速度超過該閾值,則在該方向上跳​​到下一個屏幕。

查看VelocityTracker,Scroller和Interpolator類。他們都可以幫助你。