我在检测我的应用程序中的滑动手势时遇到问题。 我的布局由一个 GridView、几个 TextView 和按钮组成。
我实现了 OnGestureListener:
public class MyActivity extends Activity implements OnGestureListener{
private GestureDetector myGesture ;
然后在 OnCreate 中:
myGesture = new GestureDetector(this);
和覆盖的方法:
@Override
public boolean onTouchEvent(MotionEvent event){
return myGesture.onTouchEvent(event);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
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) {
//right to left fling
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//left to right fling
}
} catch (Exception e) {
// nothing
}
return false;
}
这实际上很好用,但不适用于 GridView。无论在 GridView 之外的任何地方,我都可以执行此操作。在 GridView 上——绝对没有反应。 我真的不知道该怎么办,所以提前感谢您的帮助。
最佳答案
当事件被消耗时(如下所示),您是否在 onFling 中返回 true?
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
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) {
//right to left fling
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//left to right fling
}
return true;
} catch (Exception e) {
// nothing
}
return false;
}
关于Android - GridView - GestureDetector 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9487921/