How to disable RecyclerView fling but leave scroll working?

Viewed 1970

I have a horizontal RecyclerView. I need to scroll it but to disable fling by swipe gesture.

Originally all work is done at onTouchEvent method and I dont know how to disable it without rewriting all touch handling

1 Answers

You can use a GestureDetector with a SimpleOnGestureListener to capture fling events and decide whether or not to allow them.

RecyclerView recycler = findViewById(R.id.recycler);
GestureDetector detector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // return true if you want to stop the fling
        // return false if you want to allow the fling
        return true;
    }
});

recycler.setOnTouchListener((v, event) -> detector.onTouchEvent(event));

GestureDetector.OnGestureListener#onFling()

Related