Search code examples
androidandroid-recyclerviewoverscroll

How to disable overscroll effect on RecyclerView ONLY when you can't scroll anyway ?


Background

Sometimes, all items of the recyclerView are already visible to the user.

In this case, it wouldn't matter to the user to see overscroll effect, because it's impossible to actually scroll and see more items.

The problem

I know that in order to disable overscroll effect on RecyclerView, I can just use:

recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);

but I can't find out when to trigger this, when the scrolling isn't possible anyway.

The question

How can I Identify that all items are fully visible and that the user can't really scroll ?

If it helps to assume anything, I always use LinearLayoutManager (vertical and horizontal) for the RecyclerView.


Solution

  • you could give OVER_SCROLL_IF_CONTENT_SCROLLS a try. Accordingly to the documentation

    Allow a user to over-scroll this view only if the content is large enough to meaningfully scroll, provided it is a view that can scroll.

    Or you could check if you have enough items to trigger the scroll and enable/disable the over scroll mode, depending on it. Eg

    boolean notAllVisible = layoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
    if (notAllVisible) {
       recyclerView.setOverScrollMode(allVisible ? View.OVER_SCROLL_NEVER);
    }