2016-05-24 52 views
0

我有一个带有GridView的Android应用程序。手势检测器conflit onFling vs onSingleTapUp

在那个网格中,我需要检测很多事件,并且我必须使用一个GestureDetector,但是有时当我点击一个网格项时触发事件onFling而不是onSingleTapUp

我在做什么错了?

class GestureDetectorGrid extends SimpleOnGestureListener 
{ 
     /** 
     * Sliding from right to the left to move to another grid 
     */ 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, 
           float velocityX,float velocityY) 
     { 
      //my code 
      return false;  
     } 

     /** 
     * Go to another Activity by clicking on a element from the grid. 
     */ 
     @Override 
     public boolean onSingleTapUp(MotionEvent e) 
     { 
      //my code 
      return true; 
     } 

     @Override 
     public void onLongPress(MotionEvent e) 
     { 
      //my code 
      super.onLongPress(e); 
     } 

     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, 
           float distanceX, float distanceY) 
     { 
      //my code 
      return true; 
     } 

     @Override 
     public void onShowPress(MotionEvent e) 
     { 
      super.onShowPress(e); 
     } 

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

我的问题是重复的不同,因为我想知道为什么在Android GestureDetector假设onFling代替onSingleTapUp。或者如果我做错了什么。

+1

[关于网格布局拖动手势检测](的可能的复制http://stackoverflow.com/questions/937313/fling-gesture-detection-on-grid -布局) –

回答

0

我解决这样问题:

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, 
         float velocityX,float velocityY) { 

    int posIni = pointToPosition((int) e1.getX(), (int) e1.getY()); 

    int posFin = pointToPosition((int) e2.getX(), (int) e2.getY()); 

    if(posIni == posFin) { 
     //onClick code 
    } 
    else { 
     //onFling code. 
    } 
    return true;  
}