AndroidListView监听滑动事件的方法(详解)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
AndroidListView监听滑动事件的⽅法(详解)
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监听滑动事件的⽅法(详解)就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。