How can I limit the scrolling ability of a vertical Recyclerview to only allow scrolling down?
I want to make somehow a "list with no return to the top".
EDIT: It's not a duplicate. I don't want to disable scrolling vertically. I just want to disable scrolling upwards.
I figured out a solution using an OnItemTouchListener.
The scrolling event consists of 3 MotionEvents : ACTION_DOWN , ACTION_MOVE and ACTION_UP.
So on ACTION_DOWN we get the vertical position of the cursor (Y) and the on ACTION_MOVE we compare the new position to the old one.
By returning true, the method onInterceptTouchEvent() makes sure we intercept the scrolling event.
float lastY;
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN) {
lastY = event.getY();
}
if (action == MotionEvent.ACTION_MOVE && event.getY() > lastY) {
return true;
}
return false;
}
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With