I cannot disable scrolling in the RecyclerView
. I tried calling rv.setEnabled(false)
but I can still scroll.
How can I disable scrolling?
I cannot disable scrolling in the RecyclerView
. I tried calling rv.setEnabled(false)
but I can still scroll.
How can I disable scrolling?
There is a more straightforward way to disable scrolling, using just standard functionality.
RecyclerView
has the method calledaddOnScrollListener(OnScrollListener listener)
, and using just this you can stop it from scrolling, just so:Use case: Let's say that you want to disable scrolling when you click on one of the items within
RecyclerView
so you could perform some actions with it, without being distracted by accidentally scrolling to another item, and when you are done with it, just click on the item again to enable scrolling. For that, you would want to attachOnClickListener
to every item withinRecyclerView
, so when you click on an item, it would toggleisItemSelected
fromfalse
totrue
. This way when you try to scroll,RecyclerView
will automatically call methodonScrollStateChanged
and sinceisItemSelected
set totrue
, it will stop immediately, beforeRecyclerView
got the chance, well... to scroll.Note: for better usability, try to use
GestureListener
instead ofOnClickListener
to preventaccidental
clicks.This a bit hackish workaround but it works; you can enable/disable scrolling in the
RecyclerView
.This is an empty
RecyclerView.OnItemTouchListener
stealing every touch event thus disabling the targetRecyclerView
.Using it:
Another alternative is
setLayoutFrozen
, but it comes with a bunch of other side effects.https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#setLayoutFrozen(boolean)
You can disable scrolling by freezing your RecyclerView.
To freeze:
recyclerView.setLayoutFrozen(true)
To unfreeze:
recyclerView.setLayoutFrozen(false)
I know this already has an accepted answer, but the solution doesn't take into account a use-case that I came across.
I specifically needed a header item that was still clickable, yet disabled the scrolling mechanism of the RecyclerView. This can be accomplished with the following code:
The real answer is
More info in documentation