Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether a RecyclerView / LinearLayoutManager is scrolled to top or bottom?

Currently I am using the follow code to check whether SwipeRefreshLayout should be enabled.

private void laySwipeToggle() {     if (mRecyclerView.getChildCount() == 0 || mRecyclerView.getChildAt(0).getTop() == 0) {         mLaySwipe.setEnabled(true);     } else {         mLaySwipe.setEnabled(false);     } } 

But here is the problem. When it's scrolled to another item's view's boundary mRecyclerView.getChildAt(0).getTop() also returns 0.

The problem

Is there something like RecyclerView.isScrolledToBottom() or RecyclerView.isScrolledToTop()?

EDIT: (mRecyclerView.getChildAt(0).getTop() == 0 && linearLayoutManager.findFirstVisibleItemPosition() == 0) kind of does the RecyclerView.isScrolledToTop(), but what about RecyclerView.isScrolledToBottom()?

like image 232
Saren Arterius Avatar asked Jan 08 '15 13:01

Saren Arterius


People also ask

How do I know my RecyclerView is scrolling?

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 scroll to the bottom of my recycler view?

Recyclerview scroll to bottom using scrollToPositon. After setting the adapter, then call the scrollToPosition function to scroll the recycler view to the bottom.

How scroll RecyclerView to bottom in android programmatically?

mLinearLayoutManager = new LinearLayoutManager(this); recyclerView. setLayoutManager(mLinearLayoutManager); 3). On your Button onClick , do this to scroll to the bottom of your RecyclerView .


1 Answers

The solution is in the layout manager.

LinearLayoutManager layoutManager = new LinearLayoutManager(this);  // Add this to your Recycler view recyclerView.setLayoutManager(layoutManager);  // To check if at the top of recycler view if(layoutManager.firstCompletelyVisibleItemPosition()==0){     // Its at top }  // To check if at the bottom of recycler view if(layoutManager.lastCompletelyVisibleItemPosition()==data.size()-1){     // Its at bottom } 

EDIT

In case your item size is larger than the screen use the following to detect the top event.

RecyclerView recyclerView = (RecyclerView) view; LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();  int pos = linearLayoutManager.findFirstVisibleItemPosition();  if(linearLayoutManager.findViewByPosition(pos).getTop()==0 && pos==0){     return true; } 

PS: Actually, if you place the RecyclerView directly inside the SwipeRefreshview you wouldn't need to do this

like image 95
humblerookie Avatar answered Sep 28 '22 16:09

humblerookie