2011-10-14 82 views
1

我想在LinearLayout上添加一個動作。爲此我使用了下面的代碼。Android onfling prob

public class NewsActivity extends Activity { 

    .......... 
    ........... 
    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 GestureDetector gestureDetector; 
    View.OnTouchListener gestureListener; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     ..................... 
     ..................... 
     ..................... 

     LinearLayout newDeailsBlock = (LinearLayout) findViewById(R.id.newdeailsblock); 
     // Gesture detection 
     gestureDetector = new GestureDetector(new MyGestureDetector()); 
     gestureListener = new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       if (gestureDetector.onTouchEvent(event)) { 
        return true; 
       } 
       return false; 
      } 
     }; 
     newDeailsBlock.setOnTouchListener(gestureListener); 
    } 

    class MyGestureDetector extends SimpleOnGestureListener { 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      try { 
       Log.d("move","Swipe test"); 
       if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
        return false; 
       // right to left swipe 
       if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {     
        Log.d("move","Left Swipe"); 
       } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
        Toast.makeText(NewsActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show(); 
        Log.d("move","Right Swipe"); 
       } 
      } catch (Exception e) { 
       // nothing 
      } 
      return false; 
     } 

    } 
} 

但它根本不工作而且返回false。我的代碼中有什麼問題?

回答

4

嘗試讓您的Activity擴展下面的抽象類,然後實現抽象方法next()previous()以及您想要的功能。

public abstract class SwipeActivity extends Activity { 
    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 GestureDetector gestureDetector; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     gestureDetector = new GestureDetector(new SwipeDetector()); 
    } 

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

      // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH, then dismiss the swipe. 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
       return false; 

      // Swipe from right to left. 
      // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE) and a certain velocity (SWIPE_THRESHOLD_VELOCITY). 
      if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       next(); 
       return true; 
      } 

      // Swipe from left to right. 
      // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE) and a certain velocity (SWIPE_THRESHOLD_VELOCITY). 
      if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       previous(); 
       return true; 
      } 

      return false; 
     } 
    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent ev) { 
     //TouchEvent dispatcher. 
     if (gestureDetector != null) { 
      if (gestureDetector.onTouchEvent(ev)) 
       //If the gestureDetector handles the event, a swipe has been executed and no more needs to be done. 
       return true; 
     } 
     return super.dispatchTouchEvent(ev); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     return gestureDetector.onTouchEvent(event); 
    } 

    protected abstract void previous(); 
    protected abstract void next(); 
} 
+0

是的。但爲什麼它工作? –

+0

非常感謝!!!!無論窗口有什麼意見,我都想在屏幕上檢測Fling,但不影響視圖。我嘗試了很多例子。有些人無法檢測到Fling,有些人可以檢測到但不能觸摸視圖。我終於明白關鍵是'dispatchTouchEvent(MotionEvent ev)'!!! – Yeung