其他分享
首页 > 其他分享> > Android-GridView-GestureDetector不起作用

Android-GridView-GestureDetector不起作用

作者:互联网

我在应用程序中检测到挥动手势时遇到问题.
我的布局由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;
}

标签:gesturedetector,gestures,gridview,android
来源: https://codeday.me/bug/20191101/1985085.html