I have a huge RecyclerView
with GridLayoutManager
.
Sometimes there's a small number of items shown, and I need to disable scroll in this case. Scroll should be enabled only when there're too many items to be shown in a RecyclerView, so scroll is needed.
Why?
Because when a user clicks fast enough on items, the click listener is not triggered. User accidentally moves a RecyclerView a bit. Recycler's onTouchListener
gets EVENT_MOVE
and consumes the event.
I've tried to create custom GridLayoutManager with overridden canScrollVertically()
method:
override fun canScrollVertically(): Boolean {
return super.canScrollVertically() && isScrollEnabled
}
I use this function to check if the RecyclerView is scrollable:
fun RecyclerView.isScrollableVertically(): Boolean {
return computeVerticalScrollRange() > height
}
Then, I update isScrollEnable field:
val isScrollableVertically = items_list.isScrollableVertically()
(category_items_list.layoutManager as CustomGridLayoutManager).setScrollEnabled(isScrollableVertically)
But even when there are a lot of items to show, items_list.isScrollableVertically()
returns false.
I'm trying a lot of things to disable scroll when there's enough room for all items, but I haven't found a proper way to do this for a day. I will appreciate any help!