hthms hthms - 2 months ago 30
Android Question

Scrolling lag with GestureDetector onScroll

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?

Answer

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.