Currently I am using the follow code to check whether SwipeRefreshLayout should be enabled.
private void laySwipeToggle() {
if (mRecyclerView.getChildCou
you can do this it's work for me
mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ?
0 : recyclerView.getChildAt(0).getTop();
LinearLayoutManager linearLayoutManager1 = (LinearLayoutManager) recyclerView.getLayoutManager();
int firstVisibleItem = linearLayoutManager1.findFirstVisibleItemPosition();
swipeRefreshLayout.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
}
});
@Saren Arterius, Using the addOnScrollListener of RecycleView, you can find the scrolling top or bottom of Vertical RecycleView like below,
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy < 0) {
// Recycle view scrolling up...
} else if (dy > 0) {
// Recycle view scrolling down...
}
}
});
You can detect top by using the following
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(IsRecyclerViewAtTop())
{
//your recycler view reached Top do some thing
}
}
});
private boolean IsRecyclerViewAtTop() {
if(recyclerView.getChildCount() == 0)
return true;
return recyclerView.getChildAt(0).getTop() == 0;
}
This will detect top when you release the finger once reached top,
if you want to detect as soon the reacyclerview reaches top check if(IsRecyclerViewAtTop())
inside onScrolled
method
To find out if the RecyclerView is scrolled to bottom, you could reuse the methods used for the scrollbar.
This is the calculation, for convenience written as a Kotlin extension function:
fun RecyclerView.isScrolledToBottom(): Boolean {
val contentHeight = height - (paddingTop + paddingBottom)
return computeVerticalScrollRange() == computeVerticalScrollOffset() + contentHeight
}
You can try recyclerView.canScrollVertically(int direction)
, if you just need to know whether it possible to scroll or not.
Direction integers:
In order to check whether RecyclerView
is scrolled to bottom. Use the following code.
/**
* Check whether the last item in RecyclerView is being displayed or not
*
* @param recyclerView which you would like to check
* @return true if last position was Visible and false Otherwise
*/
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
return true;
}
return false;
}
Some Additional info
If you want to implement ScrollToBottom
in RecyclerView
when Edittext
is tapped
then i recommend you add 1 second delay like this:
edittext.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
if (isLastItemDisplaying(recyclerView)) {
// The scrolling can happen instantly before keyboard even opens up so to handle that we add 1 second delay to scrolling
recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
}
}, 1000);
}
return false;
}
});