Check when smoothScrollToPosition has finished
Asked Answered
F

3

8

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.");
} 
Francophile answered 10/7, 2018 at 9:10 Comment(0)
P
18

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.

Plenish answered 10/7, 2018 at 10:4 Comment(1)
RecyclerView.addOnScrollListener just adds listener in a List. So, i guess we may add the same listener several times, so it will be duplicated.So, if scrollToPosition will be called from some click listener, we must remove old onScrollListener in case it wasn't removed in onScrollStateChanged, because the desired SCROLL_STATE_IDLE did not happen yet.Plenish
F
0

You can customize LinearLayoutManager to implement onStop callback

public class XLinearLayoutManager extends LinearLayoutManager {
    private OnLayoutManagerListener mListener;

    public interface OnLayoutManagerListener {
        void onSmoothScrollStopped();
    }

    public XLinearLayoutManager(Context context) {
        super(context);
    }

    public void setLayoutManagerListener(OnLayoutManagerListener listener) {
        mListener = listener;
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        // super.smoothScrollToPosition(recyclerView, state, position);
        // clone super.smoothScrollToPosition and add onStop callback
        final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
            @Override
            protected void onStop() {
                super.onStop();
                if (mListener != null) {
                    mListener.onSmoothScrollStopped();
                }
            }
        };
        linearSmoothScroller.setTargetPosition(position);
        startSmoothScroll(linearSmoothScroller);
    }
}

and implement the listener before calling smooth scroll

// setup LayoutManager for RecyclerView
final XLinearLayoutManager layoutManager = new XLinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);

// call smoothScrollToPosition
layoutManager.setLayoutManagerListener(new OnLayoutManagerListener() {
    @Override
    public void onSmoothScrollStopped() {
        //TODO: smoothScrollToPosition finished
    }
});
recyclerView.smoothScrollToPosition(position);
Fermentative answered 9/5 at 4:23 Comment(0)
K
-2

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.

Kirstenkirsteni answered 10/7, 2018 at 9:15 Comment(1)
This method is called multiple times while recyclerView is scrolling.Plenish

© 2022 - 2024 — McMap. All rights reserved.