SoFunction
Updated on 2025-04-07

Adding a listening example of RecycleView in Android that slides down to the bottom

We often use pull-down refresh in our daily development, and the best online evaluation of open source pull-down refresh components are of course stillandroid-Ultra-Pull-To-Refresh This component can add a drop-down refresh function to any control. Of course, it also includes recycleview.

pityandroid-Ultra-Pull-To-RefreshIt only provides the function of pull-down refresh, but for the components of the list class, we use more functions of pull-up loading or sliding to the bottom to automatically load in our daily development. Of course, at present, users prefer the function of sliding to the bottom to automatically loading. For example, the recycleview mentioned today, we can only add more functions to slide to the bottom to load more.

Then its implementation principle is very simple:

There is an abstract class OnScrollListener inside the RecycleView to receive scroll events. There are two implementation methods in this class.

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){}
  }

You can understand the comments of Tongduo source code

onScrollStateChanged is called when the scroll state of the recyclerview changes.

onScrolled is called when the layout is visible and recycleview scrolls.

Then the idea is:

(1) In the onScrollStateChanged method, determine that the current scrolling state is the state where the scrolling stops.

(2) Then obtain the last visible position according to the method in the API.

(3) Determine that the number of items in the currently visible recycleview is greater than 0

(4) Determine that the last visible position is greater than the number greater than the total number of items minus one

(5) And the total number of items is greater than the visible item, so that it can be executed only when more than one interface is exceeded.

When the request for the make-face is met, we can execute our time-consuming logic through the interface callback and display the loaded dialog.

Because RecyclerView can be flexibly converted into lists, tables, and waterfalls through layoutManager. Especially when a waterfall flows, its last visible position is different, so we must obtain the corresponding last visible position according to its different layoutManager states.

Code:

 @Override
  public void onScrollStateChanged(int state) {
    if (state == RecyclerView.SCROLL_STATE_IDLE && mLoadingListener != null) {
      LayoutManager layoutManager = getLayoutManager();
      int lastVisibleItemPosition;
      if (layoutManager instanceof GridLayoutManager) {
        lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
      } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int[] into = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];
        ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(into);
        lastVisibleItemPosition = findMax(into);
      } else {
        lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
      }
      if (() > 0
          && lastVisibleItemPosition >= () - 1 && () > ()) {
          View footView = (0);
        ();
        ();
      }
    }
  }

We can obtain all the columns of the waterfall flow through the API and find the lowest column through the following method. The loaded dialog will be displayed below this column.

 private int findMax(int[] lastPositions) {
    int max = lastPositions[0];
    for (int value : lastPositions) {
      if (value > max) {
        max = value;
      }
    }
    return max;
  }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.