在具有Android 4.0.4的Samsung 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;
    }
}

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

最佳答案

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

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:







您需要在return true中添加onDown,以便触发onScroll

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

10-09 07:01