当前位置: 移动技术网 > IT编程>移动开发>Android > Android ListView监听滑动事件的方法(详解)

Android ListView监听滑动事件的方法(详解)

2019年07月24日  | 移动技术网IT编程  | 我要评论

212hh情人艺术,beitc059,淘宝网女装牛仔裤

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监听滑动事件的方法(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网