gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:29:19 31 4
gpt4 key购买 nike

我有一个自定义 View ,我想在其上设置长按监听器。我正在使用以下代码进行设置。

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);

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

gestureDetector.onTouchEvent(event);
return true;

thisthis也许解释一下原因:

Beware of creating a listener that returns false for the ACTION_DOWN event. If you do this, the listener will not be called for the subsequent ACTION_MOVE and ACTION_UP string of events. This is because ACTION_DOWN is the starting point for all touch events.

关于android - GestureDetector.onTouchEvent(MotionEvent e) 在所有手势上调用 onLongPress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32177573/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com