2012-04-04 107 views
4

我正在嘗試在我的應用中實施滑動手勢。我已經制作了幾乎所有的代碼,但它不起作用。Android - 使用滑動手勢的問題

這裏是我在我的活動代碼:

// Swipe detector 
gestureDetector = new GestureDetector(new SwipeGesture(this)); 
gestureListener = new OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     Log.e("", "It works"); 
     return gestureDetector.onTouchEvent(event); 
    } 
}; 

LinearLayout root = (LinearLayout) findViewById(R.id.rules_root); 
root.setOnTouchListener(gestureListener); 

當我觸摸屏幕時,logcat的顯示it works

他在這裏我的類SwipeGesture的代碼:

public class SwipeGesture extends SimpleOnGestureListener 
{ 
    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_MAX_OFF_PATH = 250; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

    private Activity activity; 

    public SwipeGesture(Activity activity) 
    { 
     super(); 
     this.activity = activity; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
    { 
     Log.e("", "Here I am"); 
     try 
     { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; 
      if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
      { 
       if (((TabActivity) activity.getParent()).getTabHost() != null) 
       { 
        TabHost th = ((TabActivity) activity.getParent()).getTabHost(); 
        th.setCurrentTab(th.getCurrentTab() - 1); 
       } 
       else 
       { 
        activity.finish(); 
       } 
       Log.e("", "Swipe left"); 
      } 
      else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
      { 
       if (((TabActivity) activity.getParent()).getTabHost() != null) 
       { 
        TabHost th = ((TabActivity) activity.getParent()).getTabHost(); 
        th.setCurrentTab(th.getCurrentTab() + 1); 
       } 
       Log.e("", "Swipe right"); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return false; 
    } 
} 

Log.e("", "Here I am");永遠不會顯示。所以我認爲onFling方法永遠不會被調用。

任何想法,爲什麼這不起作用?

謝謝。

問候。

V.

回答

6

在您的SimpleOnGestureListener中,重寫onDown以註冊您的手勢。它可以只返回true,但它必須是這樣定義..

@Override 
    public boolean onDown(MotionEvent e) { 
      return true; 
    } 

.... See this link.. and the comment below the answer..

+0

謝謝,這個作品! – Manitoba 2012-04-04 09:43:30

+0

哦,男人,我差不多兩個小時都遇到同樣的問題,+1謝謝你的客觀性 – 2013-05-01 13:45:42

5

您需要更改幾件事情,這裏是一個示例。

設置你的OnTouchListener

root.setOnTouchListener(new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       if (gestureDetector.onTouchEvent(event)) { 
        return false; 
       } 
       return false; 
      } 
     }); 

SwipeGesture類

class SwipeGesture extends SimpleOnGestureListener { 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 

     try { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
       return false; 
      if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
//Do something 

       return true; 
      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
//Do something 
       return true; 

      } 

     } catch (Exception e) { 
      Log.e("Fling", "There was an error processing the Fling event:" 
        + e.getMessage()); 
     } 
     return true; 
    } 

    // Necessary for the onFling event to register 
    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 
} 

它看起來像你Tabs之間滑動。使用FragmentsViewPager對您的用戶來說更容易和更順暢。