2

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

Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58

1 Answers1

1

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()

Ben P.
  • 52,661
  • 6
  • 95
  • 123