Android ListView監(jiān)聽滑動事件的方法(詳解)
ListView的主要有兩種滑動事件監(jiān)聽方法,OnTouchListener和OnScrollListener
1、OnTouchListener
OnTouchListener方法來自View中的監(jiān)聽事件,可以在監(jiān)聽三個Action事件發(fā)生時通過MotionEvent的getX()方法或getY()方法獲取到當前觸摸的坐標值,來對用戶的滑動方向進行判斷,并可在不同的Action狀態(tài)中做出相應的處理
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狀態(tài),MotionEvent類中還定義了很多其它狀態(tài),我們可以靈活的使用這些狀態(tài)
• 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中的監(jiān)聽事件,因為ListView直接繼承自AbsListView,所以在AbsListView中有很多ListView相關信息
OnScrollListener中有兩個回調方法
• public void onScrollStateChanged(AbsListView view, int scrollState):監(jiān)聽滑動狀態(tài)的改變
• public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount):監(jiān)聽滑動
在源碼中有其詳細的解釋
/**
* 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根據(jù)scrollState來決定其回調的次數(shù),它有三種模式:
• OnScrollListener.SCROLL_STATE_IDLE:滾動停止時的狀態(tài)
• OnScrollListener.SCROLL_STATE_STOUCH_SCROLL:觸摸正在滾動,手指還沒離開界面時的狀態(tài)
• OnScrollListener.SCROLL_STATE_FLING:用戶在用力滑動后,ListView由于慣性將繼續(xù)滑動時的狀態(tài)
當用戶沒有用力滑動時,OnScrollSateChanged方法只會回調2次,否則回調三次,我們在使用時通常會以設置Flag標志,來區(qū)分不同的滑動狀態(tài),從而進行相應的處理
2.2 OnScroll方法
在ListView滾動時會一直被回調,它通過里面有三個參數(shù)來顯示當前ListView的滾動狀態(tài)
• firstVisibleItem:當前能看見的第一個item的ID(從0開始)
• visibleItemCount:當前可見的item總數(shù)
• totalItemCount:列表中適配器總數(shù)量,也就是整個ListView中item總數(shù)
注意:當前可見的item總數(shù),包括屏幕中沒有顯示完整的item,如顯示一半的item也會算在可見范圍內
通過這三個參數(shù),我么可以實現(xiàn)很多事件判斷,如:
(1)判斷當前是否滑動到最后一行
當前視圖中第一個item的ID加上當前屏幕中可見item的總數(shù)如果等于ListView中所有item總數(shù)時,就表示移動到了最后一行
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
// 滾動到最后一行了
}
(2)判斷滑動的方向
通過oldVisibleItem 記錄上一次firstVisibleItem的位置,再與滑動后的firstVisibleItem進行比較,就可得知滑動的方向
if (firstVisibleItem > oldVisibleItem) {
// 向上滑動
}
if (firstVisibleItem < oldVisibleItem) {
// 向下滑動
}
oldVisibleItem = firstVisibleItem;
ListView也為我們提供了一些封裝好了的方法,來獲取item的位置信息
// 獲取當前可見區(qū)域內第一個item的id
mListView.getFirstVisiblePosition();
// 獲取當前可見區(qū)域內最后一個item的id
mListView.getLastVisiblePosition();
以上這篇Android ListView監(jiān)聽滑動事件的方法(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android ListView里控件添加監(jiān)聽方法的實例詳解
- android動態(tài)布局之動態(tài)加入TextView和ListView的方法
- Android ListView添加頭布局和腳布局實例詳解
- Android中ListView Item布局優(yōu)化技巧
- Android實現(xiàn)的ListView分組布局改進示例
- Android ListView自動顯示隱藏布局的實現(xiàn)方法
- Android自定義listview布局實現(xiàn)上拉加載下拉刷新功能
- Android App界面的ListView布局實戰(zhàn)演練
- Android Listview中顯示不同的視圖布局詳解及實例代碼
- Android開發(fā)實現(xiàn)ListView部分布局監(jiān)聽的方法
相關文章
解析android 流量監(jiān)測的實現(xiàn)原理
本篇文章是對android中流量監(jiān)測的實現(xiàn)原理進行了詳細的分析介紹,需要的朋友參考下2013-06-06
Android 廣播大全 Intent Action 事件詳解
這篇文章主要給大家介紹Android 廣播大全 Intent Action 事件詳解,涉及到android廣播action 方面知識點,本文講解的非常的全面,感興趣的朋友一起看看吧2015-10-10
Android基于TextView不獲取焦點實現(xiàn)跑馬燈效果
這篇文章主要介紹了Android基于TextView不獲取焦點實現(xiàn)跑馬燈效果,結合實例形式分析了Android基于TextView實現(xiàn)跑馬燈的功能與布局相關技巧,需要的朋友可以參考下2017-02-02
Android修行手冊之ConstraintLayout布局使用詳解
這篇文章主要為大家介紹了Android修行手冊之ConstraintLayout使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Android 開發(fā)之Dialog,Toast,Snackbar提醒
這篇文章主要介紹了Android 開發(fā)之Dialog,Toast,Snackbar提醒的相關資料,需要的朋友可以參考下2017-03-03
Android中HttpURLConnection與HttpClient的使用與封裝
這篇文章主要介紹了Android中HttpURLConnection與HttpClient的使用以及封裝方法,感興趣的小伙伴們可以參考一下2016-03-03

