怎么在Android应用中利用ListView实现一个监听滑动事件

发布时间:2020-11-27 15:45:55 作者:Leah
来源:亿速云 阅读:191

怎么在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:触摸被取消时

• 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应用中利用ListView实现一个监听滑动事件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 安卓左右滑动事件监听
  2. flutter 中监听滑动事件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

listview android roi

上一篇:在Android应用中使用Handler时出现 leak如何解决

下一篇:怎么利用java对文件名进行修改

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》