GestureDetector.onTouchEvent(MotionEvent e) 在所有手势上调用 onLongPress

2023-12-26

我有一个自定义视图,我想在其上设置长按监听器。 我正在使用以下代码来设置相同的内容。

final GestureDetector gestureDetector = (new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
    public void onLongPress(MotionEvent e) {
        Log.e("test", "Long press detected");
    }
}));

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

问题出在所有手势上,无论是单击还是双击 onLongPress 都会被调用。

我可以通过实现 onDown() 方法使代码正常工作,但为什么在未实现时它不起作用?难道 onLongPress() 不应该只在手势为 onLongPress 时调用吗?


如果有人仍然被困在这个问题上, 我在执行以下操作时发现发生了这种情况:

return gestureDetector.onTouchEvent(event);

与此相反:(麦克洛文在评论中发布的链接中也提到了)

gestureDetector.onTouchEvent(event);
return true;

this https://developer.android.com/training/gestures/detector#capture-touch-events-for-a-single-view and this https://developer.android.com/training/gestures/detector#detect-a-subset-of-supported-gestures也许可以解释一下原因:

请注意创建对 ACTION_DOWN 事件返回 false 的侦听器。如果这样做,将不会为后续的 ACTION_MOVE 和 ACTION_UP 事件字符串调用侦听器。这是因为 ACTION_DOWN 是所有触摸事件的起点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

GestureDetector.onTouchEvent(MotionEvent e) 在所有手势上调用 onLongPress 的相关文章

随机推荐