Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if RecyclerView is scrollable

How to check if a RecyclerView is scrollable, ie, there are items below/above the visible area

I have a dropdown in my recycler view which works by using notifyItemRangeInserted() and notifyItemRangeRemoved(). Whenever any of this happens, I want to check if the RecyclerView is scrollable or not, as I have to adjust another view, a banner like in newsstand, accordingly

like image 492
Sourabh Avatar asked Sep 23 '15 19:09

Sourabh


People also ask

How can I tell if my RecyclerView is scrollable?

You can use Scroll listener to detect scroll up or down changes in RecyclerView . RecycleView invokes the onScrollStateChanged() method before onScrolled() method. The onScrollStateChanged() method provides you the RecycleView's status: SCROLL_STATE_IDLE : No scrolling.

How do I know if my RecyclerView is scrolled to the bottom?

canScrollVertically(1) && dy > 0) { //scrolled to BOTTOM }else if (! recyclerView. canScrollVertically(-1) && dy < 0) { //scrolled to TOP } } }); This is simple and will hit exactly one time under all conditions when you have scrolled to the top or bottom.


4 Answers

There you go:

public boolean isRecyclerScrollable() {
  LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
  RecyclerView.Adapter adapter = recyclerView.getAdapter();
  if (layoutManager == null || adapter == null) return false;

  return layoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
}
like image 178
Simas Avatar answered Oct 22 '22 08:10

Simas


I found an easy solution that works regardless of the position you are in the list:

public boolean isRecyclerScrollable(RecyclerView recyclerView) {
    return recyclerView.computeHorizontalScrollRange() > recyclerView.getWidth() || recyclerView.computeVerticalScrollRange() > recyclerView.getHeight();
}
like image 23
Fabio Picchi Avatar answered Oct 22 '22 07:10

Fabio Picchi


The idea is to check if the last completely visible element is the last element in the list.

private boolean isScrollable()
{
    return mLinearLayoutManager.findLastCompletelyVisibleItemPosition() + 1 ==
        mRecyclerViewAdapter.getItemCount();
}
like image 3
Sotti Avatar answered Oct 22 '22 06:10

Sotti


This one works for me.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);


            boolean isRecylerScrollable = recyclerView.computeHorizontalScrollRange() > recyclerView.getWidth();

            if(isRecylerScrollable){
             //do something
            }
    });
}
like image 3
shinta Avatar answered Oct 22 '22 06:10

shinta