国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android ListView監(jiān)聽滑動(dòng)事件的方法(詳解)

2019-10-23 18:34:53
字體:
供稿:網(wǎng)友

ListView的主要有兩種滑動(dòng)事件監(jiān)聽方法,OnTouchListener和OnScrollListener

1、OnTouchListener

OnTouchListener方法來自View中的監(jiān)聽事件,可以在監(jiān)聽三個(gè)Action事件發(fā)生時(shí)通過MotionEvent的getX()方法或getY()方法獲取到當(dāng)前觸摸的坐標(biāo)值,來對用戶的滑動(dòng)方向進(jìn)行判斷,并可在不同的Action狀態(tài)中做出相應(yīng)的處理

mListView.setOnTouchListener(new View.OnTouchListener() {      @Override      public boolean onTouch(View v, MotionEvent event) {        switch (event.getAction()) {          case MotionEvent.ACTION_DOWN:            // 觸摸按下時(shí)的操作            break;          case MotionEvent.ACTION_MOVE:            // 觸摸移動(dòng)時(shí)的操作            break;          case MotionEvent.ACTION_UP:            // 觸摸抬起時(shí)的操作            break;        }        return false;      } });

 

不僅僅只有上面的三種Action狀態(tài),MotionEvent類中還定義了很多其它狀態(tài),我們可以靈活的使用這些狀態(tài)

• MotionEvent.ACTION_DOWN:開始觸摸

• MotionEvent.ACTION_MOVE:觸摸移動(dòng)

• MotionEvent.ACTION_UP:觸摸抬起

• MotionEvent.ACTION_OUTSIDE:觸摸范圍超過了UI邊界

• MotionEvent.ACTION_CANCEL:觸摸被取消時(shí)

• MotionEvent.ACTION_POINTER_DOWN:當(dāng)有另外一個(gè)觸摸按下時(shí)(多點(diǎn)觸摸)

• MotionEvent.ACTION_POINTER_UP:當(dāng)另一個(gè)觸摸抬起時(shí)(多點(diǎn)觸摸)

2、OnScrollListener

OnScrollListener來自AbsListView中的監(jiān)聽事件,因?yàn)長istView直接繼承自AbsListView,所以在AbsListView中有很多ListView相關(guān)信息

OnScrollListener中有兩個(gè)回調(diào)方法

• public void onScrollStateChanged(AbsListView view, int scrollState):監(jiān)聽滑動(dòng)狀態(tài)的改變

• public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount):監(jiān)聽滑動(dòng)

在源碼中有其詳細(xì)的解釋

  /**   * 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來決定其回調(diào)的次數(shù),它有三種模式:

• OnScrollListener.SCROLL_STATE_IDLE:滾動(dòng)停止時(shí)的狀態(tài)

• OnScrollListener.SCROLL_STATE_STOUCH_SCROLL:觸摸正在滾動(dòng),手指還沒離開界面時(shí)的狀態(tài)

• OnScrollListener.SCROLL_STATE_FLING:用戶在用力滑動(dòng)后,ListView由于慣性將繼續(xù)滑動(dòng)時(shí)的狀態(tài)

當(dāng)用戶沒有用力滑動(dòng)時(shí),OnScrollSateChanged方法只會(huì)回調(diào)2次,否則回調(diào)三次,我們在使用時(shí)通常會(huì)以設(shè)置Flag標(biāo)志,來區(qū)分不同的滑動(dòng)狀態(tài),從而進(jìn)行相應(yīng)的處理

2.2 OnScroll方法

在ListView滾動(dòng)時(shí)會(huì)一直被回調(diào),它通過里面有三個(gè)參數(shù)來顯示當(dāng)前ListView的滾動(dòng)狀態(tài)

• firstVisibleItem:當(dāng)前能看見的第一個(gè)item的ID(從0開始)

• visibleItemCount:當(dāng)前可見的item總數(shù)

• totalItemCount:列表中適配器總數(shù)量,也就是整個(gè)ListView中item總數(shù)

注意:當(dāng)前可見的item總數(shù),包括屏幕中沒有顯示完整的item,如顯示一半的item也會(huì)算在可見范圍內(nèi)

通過這三個(gè)參數(shù),我么可以實(shí)現(xiàn)很多事件判斷,如:

(1)判斷當(dāng)前是否滑動(dòng)到最后一行

當(dāng)前視圖中第一個(gè)item的ID加上當(dāng)前屏幕中可見item的總數(shù)如果等于ListView中所有item總數(shù)時(shí),就表示移動(dòng)到了最后一行

if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {// 滾動(dòng)到最后一行了}

 

(2)判斷滑動(dòng)的方向

通過oldVisibleItem 記錄上一次firstVisibleItem的位置,再與滑動(dòng)后的firstVisibleItem進(jìn)行比較,就可得知滑動(dòng)的方向

if (firstVisibleItem > oldVisibleItem) {// 向上滑動(dòng)}if (firstVisibleItem < oldVisibleItem) {// 向下滑動(dòng)}oldVisibleItem = firstVisibleItem;ListView也為我們提供了一些封裝好了的方法,來獲取item的位置信息// 獲取當(dāng)前可見區(qū)域內(nèi)第一個(gè)item的idmListView.getFirstVisiblePosition();// 獲取當(dāng)前可見區(qū)域內(nèi)最后一個(gè)item的idmListView.getLastVisiblePosition();

以上這篇Android ListView監(jiān)聽滑動(dòng)事件的方法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大名县| 晋州市| 大庆市| 宿松县| 德化县| 石泉县| 遵化市| 葵青区| 台北县| 岳阳市| 湘潭县| 乡宁县| 山西省| 新民市| 龙州县| 拜城县| 郧西县| 维西| 金门县| 海淀区| 达尔| 揭西县| 会东县| 察隅县| 阳原县| 蚌埠市| 连州市| 清原| 旅游| 山阳县| 渝北区| 桐乡市| 泉州市| 哈巴河县| 孟村| 集贤县| 杭州市| 九台市| 文山县| 宝鸡市| 遂川县|