问题
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.");
}
回答1:
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.
回答2:
this may be what you are looking for.
Basically you just have to implement the listener in the recycler that you set
Listener scroll Recycler
I hope I have helped.
UPDATE: instead of using
recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
// do something...
}
});
you must use
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mana) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
}
});
I personally would like to use
Pull to refresh
回答3:
There is a listener for it:
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.OnScrollListener
void onScrolled (RecyclerView recyclerView,
int dx,
int dy)
Callback method to be invoked when the RecyclerView has been scrolled. This will be called after the scroll has completed.
来源:https://stackoverflow.com/questions/51261327/check-when-smoothscrolltoposition-has-finished