I would like to check when smoothScrollToPosition
has finished scrolling back to the first item of recyclerview
. I tried doing it like this which only works while smoothScrollToPosition is still scrolling:
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView,new RecyclerView.State(), 0);
if (!recyclerView.getLayoutManager().isSmoothScrolling()) {
Log.d(TAG, "Scrolling has ended.");
}
I use onScrollStateChanged(RecyclerView recyclerView, int newState)
method for tracking scrolling state. The method to initiate scroll looks like this:
private void scrollToPosition(int position){
recyclerView.removeOnScrollListener(onScrollListener);
recyclerView.addOnScrollListener(onScrollListener);
recyclerView.smoothScrollToPosition(position);
}
And here is the listener:
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
switch (newState) {
case SCROLL_STATE_IDLE:
//we reached the target position
recyclerView.removeOnScrollListener(this);
break;
}
}
};
So, when recyclerView reaches SCROLL_STATE_IDLE, that means it has finished scrolling. Don't forget to remove listener in this state, so it won't trigger on the next scroll.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With