Android RecyclerView's scroll event OnScrollListener detailed

(1) Scroll event classification

The scrolling of the list is generally divided into two types:

1. Press your finger -> drag your finger to move the list -> stop dragging your finger -> lift your finger
2. Press your finger -> drag your finger quickly and then lift your finger -> the list continues to scroll -> stop scrolling

The state of the above process changes as follows:

1. Stationary -> Forced to drag and move -> Stationary
2. Stationary -> Forced to drag and move -> Roll by itself -> Stationary

(2) Monitor the scrolling of RecyclerView

There are two ways to listen for scroll events:

1.setOnScrollListener(OnScrollListener listener)
2.addOnScrollListener(OnScrollListener listener)

where setOnScrollListener is obsolete due to the risk of possible null pointers. It is recommended to use addOnScrollListener.
(3) OnScrollListener

/**

  • An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event
  • has occurred on that RecyclerView.
  • @see RecyclerView#addOnScrollListener(OnScrollListener)
  • @see RecyclerView#clearOnChildAttachStateChangeListeners()

/
public abstract static class OnScrollListener {
/
*
* Callback method to be invoked when RecyclerView’s scroll state changes.
*
* @param recyclerView The RecyclerView whose scroll state has changed.
* @param newState The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
* {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
*/
public void onScrollStateChanged(RecyclerView recyclerView, int newState){}

/**
 * Callback method to be invoked when the RecyclerView has been scrolled. This will be
 * called after the scroll has completed.
 * <p>
 * This callback will also be called if visible item range changes after a layout
 * calculation. In that case, dx and dy will be 0.
 *
 * @param recyclerView The RecyclerView which scrolled.
 * @param dx The amount of horizontal scroll.
 * @param dy The amount of vertical scroll.
 */
public void onScrolled(RecyclerView recyclerView, int dx, int dy){}

}

The OnScrollListener class is an abstract class with two methods:

void onScrollStateChanged(RecyclerView recyclerView, int newState): Callback when scroll state changes
void onScrolled(RecyclerView recyclerView, int dx, int dy): Callback when scrolling

3.1 onScrollStateChanged(RecyclerView recyclerView, int newState)方法

The meaning of the two variables of the callback:
recyclerView: the currently scrolling RecyclerView
newState: the current scroll state.

where newState has three values:

/**

  • The RecyclerView is not currently scrolling.
    * /
    Public static final int SCROLL_STATE_IDLE = 0;

/**

  • The RecyclerView is currently being dragged by outside input such as user touch input.
    * (being dragged by the outside, usually the user is scrolling with his finger)
    */
    public static final int SCROLL_STATE_DRAGGING = 1;

/**

  • The RecyclerView is currently animating to a final position while not under outside control.
    *(自动滚动)
    */
    public static final int SCROLL_STATE_SETTLING = 2;

3.2 onScrolled(RecyclerView recyclerView, int dx, int dy)方法

The meaning of the three variables of the callback:
recyclerView : the currently scrolled view
dx : the horizontal scrolling distance
dy : the vertical scrolling distance

When dx > 0, the finger scrolls to the left, and the list scrolls to display the content on the right.
When dx < 0, the finger scrolls to the right. When the list scrolls to display the content on the left,
dy > 0, the finger scrolls up, and the list scrolls to display the following content.
dy < 0 When the finger scrolls down, the list scrolls to display the above content
(4) canScrollVertically and canScrollHorizontally methods

public boolean canScrollVertically (int direction)
This method is to judge whether the View can still slide up and down in the vertical direction.
Among them, the direction is -1 means the finger slides down (the screen slides up), and 1 means the finger slides up (the screen slides down).

public boolean canScrollHorizontally (int direction)
This method is used to judge the horizontal sliding

For example:
The value of RecyclerView.canScrollVertically(1) indicates whether it can scroll down, false indicates that it has scrolled to the bottom,
and the value of RecyclerView.canScrollVertically(-1) indicates whether it can scroll up, and false indicates that it has scrolled to the top
(5) Two judgments Whether to the bottom method:
5.1 Method 1:

If
the position of the current first visible item + the number of currently visible items >= the total number of items,
it can be judged that it is at the bottom.

loadingMoreListener = 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);

        if (dy > 0) //向下滚动
        {
            int visibleItemCount = mLinearLayoutManager.getChildCount();
            int totalItemCount = mLinearLayoutManager.getItemCount();
            int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();

            if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                loading = true;
                loadMoreDate();
            }
        }
    }

};

Determine whether it is the bottom by
visibleItemCount + pastVisiblesItems) >= totalItemCount . 5.2 Method 2:

Judging by canScrollVertically

loadingMoreListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(!loading && !recyclerView.canScrollVertically(1)){
loading = true;
loadMoreDate();
}
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

// if (dy > 0) //向下滚动
// {
// int visibleItemCount = mLinearLayoutManager.getChildCount();
// int totalItemCount = mLinearLayoutManager.getItemCount();
// int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();
//
// if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
// loading = true;
// loadMoreDate();
// }
// }
}
};

Guess you like

Origin blog.csdn.net/baidu_41666295/article/details/103149512