Android ListView监听滑动事件的方法

ListView的主要有两种滑动事件监听方法,OnTouchListener和OnScrollListener

1.OnTouchListener

OnTouchListener方法来自View中的监听事件,可以在监听三个Action事件发生时通过MotionEvent的getX()方法或getY()方法获取到当前触摸的坐标值,来对用户的滑动方向进行判断,并可在不同的Action状态中做出相应的处理

        mListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // 触摸按下时的操作

                        break;
                    case MotionEvent.ACTION_MOVE:
                        // 触摸移动时的操作

                        break;
                    case MotionEvent.ACTION_UP:
                        // 触摸抬起时的操作

                        break;
                }
                return false;
            }
        });

不仅仅只有上面的三种Action状态,MotionEvent类中还定义了很多其它状态,我们可以灵活的使用这些状态

  • MotionEvent.ACTION_DOWN:开始触摸
  • MotionEvent.ACTION_MOVE:触摸移动
  • MotionEvent.ACTION_UP:触摸抬起
  • MotionEvent.ACTION_OUTSIDE:触摸范围超过了UI边界
  • MotionEvent.ACTION_CANCEL:触摸被取消时(详见:http://stackoverflow.com/questions/11960861/what-causes-a-motionevent-action-cancel-in-android)
  • MotionEvent.ACTION_POINTER_DOWN:当有另外一个触摸按下时(多点触摸)
  • MotionEvent.ACTION_POINTER_UP:当另一个触摸抬起时(多点触摸)

2.OnScrollListener

OnScrollListener来自AbsListView中的监听事件,因为ListView直接继承自AbsListView,所以在AbsListView中有很多ListView相关信息
OnScrollListener中有两个回调方法

  • public void onScrollStateChanged(AbsListView view, int scrollState):监听滑动状态的改变
  • public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount):监听滑动

在源码中有其详细的解释

    /**
     * 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方法

OnScrollSateChanged根据scrollState来决定其回调的次数,它有三种模式:

  • OnScrollListener.SCROLL_STATE_IDLE:滚动停止时的状态
  • OnScrollListener.SCROLL_STATE_STOUCH_SCROLL:触摸正在滚动,手指还没离开界面时的状态
  • OnScrollListener.SCROLL_STATE_FLING:用户在用力滑动后,ListView由于惯性将继续滑动时的状态

当用户没有用力滑动时,OnScrollSateChanged方法只会回调2次,否则回调三次,我们在使用时通常会以设置Flag标志,来区分不同的滑动状态,从而进行相应的处理

2.2 OnScroll方法

在ListView滚动时会一直被回调,它通过里面有三个参数来显示当前ListView的滚动状态

  • firstVisibleItem:当前能看见的第一个item的ID(从0开始
  • visibleItemCount:当前可见的item总数
  • totalItemCount:列表中适配器总数量,也就是整个ListView中item总数

注意:当前可见的item总数,包括屏幕中没有显示完整的item,如显示一半的item也会算在可见范围内

通过这三个参数,我么可以实现很多事件判断,如:
(1)判断当前是否滑动到最后一行
当前视图中第一个item的ID加上当前屏幕中可见item的总数如果等于ListView中所有item总数时,就表示移动到了最后一行

                if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
                    // 滚动到最后一行了
                }

(2)判断滑动的方向
通过oldVisibleItem 记录上一次firstVisibleItem的位置,再与滑动后的firstVisibleItem进行比较,就可得知滑动的方向

                if (firstVisibleItem > oldVisibleItem) {
                    // 向上滑动
                }
                if (firstVisibleItem < oldVisibleItem) {
                    // 向下滑动
                }
                oldVisibleItem = firstVisibleItem;

ListView也为我们提供了一些封装好了的方法,来获取item的位置信息

        // 获取当前可见区域内第一个item的id
        mListView.getFirstVisiblePosition();

        // 获取当前可见区域内最后一个item的id
        mListView.getLastVisiblePosition();

你可能感兴趣的:(Android技术)