gpt4 book ai didi

android - 三星 Galaxy Note 10.1 上的 GestureDetector 未调用 OnGestureListener#onScroll

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:44 25 4
gpt4 key购买 nike

在装有 Android 4.0.4 的三星 Galaxy Note 10.1 上,当两根手指放在屏幕上时,GestureDetector 不会触发 OnGestureListener#onScroll(它会触发一根手指) .这适用于其他设备。在我的应用程序中,我只想在涉及至少两个手指时启用滚动。

这是重现现象的 View 实现:

public class MyView extends View {

GestureDetector scrollGestureDetector;

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);

scrollGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(final MotionEvent e1, final MotionEvent e2, final float distanceX, final float distanceY) {
System.out.println("SCROLL " + distanceX + ", " + distanceY);
return true;
}
});
}

@Override
public boolean onTouchEvent(MotionEvent event) {
scrollGestureDetector.onTouchEvent(event);
return true;
}
}

这种行为是否已知/记录/需要?是否有已知的解决方法?

最佳答案

您需要在 GestureDetector.SimpleOnGestureListener 中再实现一种方法 onDown,如下所示:

scrollGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(final MotionEvent e1, final MotionEvent e2, final float distanceX, final float distanceY) {
System.out.println("SCROLL " + distanceX + ", " + distanceY);
return true;
}

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

});

因为根据this documentthis guide :

Notified when a tap occurs with the down MotionEvent that triggered it. This will be triggered immediately for every down event. All other events should be preceded by this.

Whether or not you use GestureDetector.OnGestureListener, it's best practice to implement an onDown() method that returns true. This is because all gestures begin with an onDown() message. If you return false from onDown(), as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. This has the potential to cause unexpected problems in your app. The only time you should return false from onDown() is if you truly want to ignore an entire gesture.

需要在onDown返回true,这样onScroll才会被触发。

关于android - 三星 Galaxy Note 10.1 上的 GestureDetector 未调用 OnGestureListener#onScroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20287218/

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