8

I use GestureDetector to implement scrolling inside a custom View. My implementation is based on this: Smooth scrolling with inertia and edge resistance/snapback

I noticed a short pause before the scrolling starts: I examined the onScroll messages and noticed that the first one arrives only after a larger movement of a finger, which causes noticable lag at the beginning of the scrolling. After that the scrolling is smooth.

It seems GestureDetector starts sending onScroll messages only after a minimal distance between the motionevents to make sure the gesture is not a longtap or tap (btw I set setIsLongpressEnabled(false)).

Is there any way to change this behaviour and create a smooth scroll without implementing a custom scroll gesture using low level touch events?

2 Answers 2

11

The answer is no, you have to create your own GestureDetector. If you look at the Android source code (GestureDetector.java) lines 524 to 540 are use to detect the "touch slop" for a single tap. Specifically line 528 prevents the onScroll event from being called until the movement is outside the touch slop (which is pulled from the view configuration). You cannot change the view configuration and the slop is hard coded at 16 pixels. This is the radius that causes the lag that you are seeing.

1
  • 1
    Or you just set mTouchSlopSquare with reflection after the constructor (do this only if you care about onScroll and nothing else). private fun GestureDetector.hackTouchSlop() { try { val touchSlopField = javaClass.getDeclaredField("mTouchSlopSquare") touchSlopField.isAccessible = true touchSlopField.set(this, 0) } catch (ignored: Throwable) { } } Jul 19, 2018 at 10:25
0

You can use reflection to change mTouchSlopSquare from GestureDetector.java

    public static void setGestureDetectorTouchSlop(GestureDetector gestureDetector, int value) {
        try {
            Field f_mTouchSlopSquare = GestureDetector.class.getDeclaredField("mTouchSlopSquare");
            f_mTouchSlopSquare.setAccessible(true);
            f_mTouchSlopSquare.setInt(gestureDetector, value * value);
        } catch (NoSuchFieldException | IllegalAccessException | NullPointerException e) {
            Log.w(TAG, gestureDetector.toString(), e);
        }
    }

Also, here is the method to change the slop for the GestureDetectorCompat.java

    public static void setGestureDetectorTouchSlop(GestureDetectorCompat gestureDetector, int value) {
        try {
            Field f_mImpl = GestureDetectorCompat.class.getDeclaredField("mImpl");
            f_mImpl.setAccessible(true);
            Object mImpl = f_mImpl.get(gestureDetector);
            if (mImpl == null) {
                Log.w(TAG, f_mImpl + " is null");
                return;
            }
            Class<?> c_GDCIJellybeanMr2 = null;
            Class<?> c_GDCIBase = null;
            try {
                c_GDCIJellybeanMr2 = Class.forName(GestureDetectorCompat.class.getName() + "$GestureDetectorCompatImplJellybeanMr2");
                c_GDCIBase = Class.forName(GestureDetectorCompat.class.getName() + "$GestureDetectorCompatImplBase");
            } catch (ClassNotFoundException ignored) {
            }
            if (c_GDCIJellybeanMr2 != null && c_GDCIJellybeanMr2.isInstance(mImpl)) {
                Field f_mDetector = c_GDCIJellybeanMr2.getDeclaredField("mDetector");
                f_mDetector.setAccessible(true);

                Object mDetector = f_mDetector.get(mImpl);
                if (mDetector instanceof GestureDetector)
                    setGestureDetectorTouchSlop((GestureDetector) mDetector, value);
            } else if (c_GDCIBase != null) {
                Field f_mTouchSlopSquare = c_GDCIBase.getDeclaredField("mTouchSlopSquare");
                f_mTouchSlopSquare.setAccessible(true);
                f_mTouchSlopSquare.setInt(mImpl, value * value);
            } else {
                Log.w(TAG, "not handled: " + mImpl.getClass().toString());
            }
        } catch (NoSuchFieldException | IllegalAccessException | NullPointerException e) {
            Log.w(TAG, gestureDetector.getClass().toString(), e);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.