SoFunction
Updated on 2025-03-11

How to listen for sliding events in Android ListView (detailed explanation)

ListView has two main sliding event monitoring methods, OnTouchListener and OnScrollListener

1、OnTouchListener

The OnTouchListener method comes from a listening event in the View. When listening for three Action events, you can obtain the coordinate value of the current touch through the getX() method or getY() method of the MotionEvent to judge the user's sliding direction, and can perform corresponding processing in different Action states.

(new () {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        switch (()) {
          case MotionEvent.ACTION_DOWN:
            // The operation when touching and pressing
            break;
          case MotionEvent.ACTION_MOVE:
            // Operation when touching and moving
            break;
          case MotionEvent.ACTION_UP:
            // Operation when touching and lifting
            break;
        }
        return false;
      }
 });

Not only the above three Action states, but also many other states are defined in the MotionEvent class, and we can use these states flexibly.

• MotionEvent.ACTION_DOWN:Start touching

• MotionEvent.ACTION_MOVE:Touch Move

• MotionEvent.ACTION_UP:Touch and lift

• MotionEvent.ACTION_OUTSIDE:Touch range exceeds UI boundaries

• MotionEvent.ACTION_CANCEL:When the touch is cancelled

• MotionEvent.ACTION_POINTER_DOWN:When another touch is pressed (multi-touch)

• MotionEvent.ACTION_POINTER_UP:When another touch is raised (multi-touch)

2、OnScrollListener

OnScrollListener comes from listening events in AbsListView. Because ListView directly inherits from AbsListView, there is a lot of ListView-related information in AbsListView.

There are two callback methods in OnScrollListener

• public void onScrollStateChanged(AbsListView view, int scrollState):Listen to the change of sliding state

• public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount):Listening to slide

There is a detailed explanation in the source code

  /**
   * Interface definition for a callback to be invoked when the list or grid
   * has been scrolled.
   */
  public interface OnScrollListener {

    /**
     * The view is not scrolling. Note navigating the list using the trackball counts as
     * being in the idle state since these transitions are not animated.
     */
    public static int SCROLL_STATE_IDLE = 0;

    /**
     * The user is scrolling using touch, and their finger is still on the screen
     */
    public static int SCROLL_STATE_TOUCH_SCROLL = 1;

    /**
     * The user had previously been scrolling using touch and had performed a fling. The
     * animation is now coasting to a stop
     */
    public static int SCROLL_STATE_FLING = 2;

    /**
     * Callback method to be invoked while the list view or grid view is being scrolled. If the
     * view is being scrolled, this method will be called before the next frame of the scroll is
     * rendered. In particular, it will be called before any calls to
     * {@link Adapter#getView(int, View, ViewGroup)}.
     *
     * @param view The view whose scroll state is being reported
     *
     * @param scrollState The current scroll state. One of
     * {@link #SCROLL_STATE_TOUCH_SCROLL} or {@link #SCROLL_STATE_IDLE}.
     */
    public void onScrollStateChanged(AbsListView view, int scrollState);

    /**
     * Callback method to be invoked when the list or grid has been scrolled. This will be
     * called after the scroll has completed
     * @param view The view whose scroll state is being reported
     * @param firstVisibleItem the index of the first visible cell (ignore if
     *    visibleItemCount == 0)
     * @param visibleItemCount the number of visible cells
     * @param totalItemCount the number of items in the list adaptor
     */
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
        int totalItemCount);
  }

2.1 OnScrollSateChanged method

OnScrollSateChanged determines the number of callbacks based on scrollState, and it has three modes:

• OnScrollListener.SCROLL_STATE_IDLE:The status when the scroll stops

• OnScrollListener.SCROLL_STATE_STOUCH_SCROLL:The state of the touch is scrolling and the finger has not left the interface yet

• OnScrollListener.SCROLL_STATE_FLING:After the user slides hard, the state of ListView will continue to slide due to inertia.

When the user does not slide hard, the OnScrollSateChanged method will only callback 2 times, otherwise it will callback three times. When we use it, we usually set the Flag flag to distinguish different sliding states, so as to perform corresponding processing

2.2 OnScroll method

When the ListView is scrolled, it will be called back all the time. It displays the scrolling status of the current ListView through three parameters inside.

• firstVisibleItem:The ID of the first item that can be seen (start from 0)

• visibleItemCount:The total number of items currently visible

• totalItemCount:The total number of adapters in the list, that is, the total number of items in the entire ListView

Note: The total number of items currently visible, including the complete items not displayed on the screen. If half of the items are displayed, it will also be counted within the visible range.

Through these three parameters, I can achieve many event judgments, such as:

(1) Determine whether it currently slides to the last line

If the ID of the first item in the current view plus the total number of visible items in the current screen is equal to the total number of all items in the ListView, it means that it has moved to the last line.

if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
// Scroll to the last line}

(2) Determine the direction of sliding

Record the position of the last firstVisibleItem through oldVisibleItem, and then compare it with the slide firstVisibleItem to know the sliding direction

if (firstVisibleItem > oldVisibleItem) {
// Swipe up}
if (firstVisibleItem < oldVisibleItem) {
// Swipe down}
oldVisibleItem = firstVisibleItem;

ListViewWe also provide some methods for packaging,Come and get ititemLocation information

// Get the id of the first item in the currently visible area();

// Get the id of the last item in the currently visible area();

The above method of monitoring sliding events on Android ListView (detailed explanation) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.