How to know whether a RecyclerView / LinearLayoutManager is scrolled to top or bottom?
Solution 1:
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
Solution 2:
You can try recyclerView.canScrollVertically(int direction)
, if you just need to know whether it possible to scroll or not.
Direction integers:
- -1 for up
- 1 for down
- 0 will always return false.