android – OnGestureListener#onScroll未在三星Galaxy Note 10.1上从GestureDetector调用

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

这是重现现象的视图实现:

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 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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...