RecyclerView的滾動(dòng)事件OnScrollListener解析

(1)滾動(dòng)事件分類

列表的滾動(dòng)一般分為兩種:

 1.手指按下 -> 手指拖拽列表移動(dòng) -> 手指停止拖拽 -> 抬起手指
 2.手指按下 -> 手指快速拖拽后抬起手指 -> 列表繼續(xù)滾動(dòng) -> 停止?jié)L動(dòng)

上面的過程的狀態(tài)變化如下:

1.靜止 -> 被迫拖拽移動(dòng) -> 靜止
2.靜止 -> 被迫拖拽移動(dòng) -> 自己滾動(dòng) -> 靜止

(2)監(jiān)聽RecyclerView的滾動(dòng)

有兩種方式可以監(jiān)聽滾動(dòng)事件:

  1.setOnScrollListener(OnScrollListener listener)
  2.addOnScrollListener(OnScrollListener listener)

其中 setOnScrollListener 由于可能出現(xiàn)空指針的風(fēng)險(xiǎn),已經(jīng)過時(shí)。建議用addOnScrollListener。

(3)OnScrollListener

/**
 * An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event
 * has occurred on that RecyclerView.
 * <p>
 * @see RecyclerView#addOnScrollListener(OnScrollListener)
 * @see RecyclerView#clearOnChildAttachStateChangeListeners()
 *
 */
public abstract static class OnScrollListener {
    /**
     * Callback method to be invoked when RecyclerView's scroll state changes.
     *
     * @param recyclerView The RecyclerView whose scroll state has changed.
     * @param newState     The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
     *                     {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
     */
    public void onScrollStateChanged(RecyclerView recyclerView, int newState){}

    /**
     * Callback method to be invoked when the RecyclerView has been scrolled. This will be
     * called after the scroll has completed.
     * <p>
     * This callback will also be called if visible item range changes after a layout
     * calculation. In that case, dx and dy will be 0.
     *
     * @param recyclerView The RecyclerView which scrolled.
     * @param dx The amount of horizontal scroll.
     * @param dy The amount of vertical scroll.
     */
    public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
}

OnScrollListener類是個(gè)抽象類,有兩個(gè)方法:

void onScrollStateChanged(RecyclerView recyclerView, int newState): 滾動(dòng)狀態(tài)變化時(shí)回調(diào)
void onScrolled(RecyclerView recyclerView, int dx, int dy): 滾動(dòng)時(shí)回調(diào)

3.1 onScrollStateChanged(RecyclerView recyclerView, int newState)方法

回調(diào)的兩個(gè)變量的含義:
recyclerView: 當(dāng)前在滾動(dòng)的RecyclerView
newState: 當(dāng)前滾動(dòng)狀態(tài).

其中newState有三種值:

/**
 * The RecyclerView is not currently scrolling.(靜止沒有滾動(dòng))
 */
public static final int SCROLL_STATE_IDLE = 0;

/**
 * The RecyclerView is currently being dragged by outside input such as user touch input.
 *(正在被外部拖拽,一般為用戶正在用手指滾動(dòng))
 */
public static final int SCROLL_STATE_DRAGGING = 1;

/**
 * The RecyclerView is currently animating to a final position while not under outside control.
 *(自動(dòng)滾動(dòng))
 */
public static final int SCROLL_STATE_SETTLING = 2;

3.2 onScrolled(RecyclerView recyclerView, int dx, int dy)方法

回調(diào)的三個(gè)變量含義:
recyclerView : 當(dāng)前滾動(dòng)的view
dx : 水平滾動(dòng)距離
dy : 垂直滾動(dòng)距離

dx > 0 時(shí)為手指向左滾動(dòng),列表滾動(dòng)顯示右面的內(nèi)容
dx < 0 時(shí)為手指向右滾動(dòng),列表滾動(dòng)顯示左面的內(nèi)容
dy > 0 時(shí)為手指向上滾動(dòng),列表滾動(dòng)顯示下面的內(nèi)容
dy < 0 時(shí)為手指向下滾動(dòng),列表滾動(dòng)顯示上面的內(nèi)容

(4)canScrollVertically和canScrollHorizontally方法

public boolean canScrollVertically (int direction)
這個(gè)方法是判斷View在豎直方向是否還能向上,向下滑動(dòng)。
其中,direction為 -1 表示手指向下滑動(dòng)(屏幕向上滑動(dòng)), 1 表示手指向上滑動(dòng)(屏幕向下滑動(dòng))。

public boolean canScrollHorizontally (int direction)
這個(gè)方法用來判斷 水平方向的滑動(dòng)

例如:
RecyclerView.canScrollVertically(1)的值表示是否能向下滾動(dòng),false表示已經(jīng)滾動(dòng)到底部
RecyclerView.canScrollVertically(-1)的值表示是否能向上滾動(dòng),false表示已經(jīng)滾動(dòng)到頂部

(5)兩種判斷是否到底部的方法:

5.1方法一:

如果 當(dāng)前
第一個(gè)可見item的位置 + 當(dāng)前可見的item個(gè)數(shù) >= item的總個(gè)數(shù)
這樣就可以判斷出來,是在底部了。

loadingMoreListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (dy > 0) //向下滾動(dòng)
            {
                int visibleItemCount = mLinearLayoutManager.getChildCount();
                int totalItemCount = mLinearLayoutManager.getItemCount();
                int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();

                if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                    loading = true;
                    loadMoreDate();
                }
            }
        }
};

通過
visibleItemCount + pastVisiblesItems) >= totalItemCount
來判斷是否是底部。

5.2方法二:

通過canScrollVertically 來判斷

loadingMoreListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if(!loading && !recyclerView.canScrollVertically(1)){
            loading = true;
            loadMoreDate();
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

//                if (dy > 0) //向下滾動(dòng)
//                {
//                    int visibleItemCount = mLinearLayoutManager.getChildCount();
//                    int totalItemCount = mLinearLayoutManager.getItemCount();
//                    int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();
//
//                    if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
//                        loading = true;
//                        loadMoreDate();
//                    }
//                }
    }
};

參考:

http://blog.devwiki.net/index.php/2016/06/13/RecyclerView-Scroll-Listener.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • (1)滾動(dòng)事件分類 列表的滾動(dòng)一般分為兩種: 上面的過程的狀態(tài)變化如下: (2)監(jiān)聽RecyclerView的滾動(dòng)...
    風(fēng)再起時(shí)ME閱讀 79,562評(píng)論 9 79
  • 簡(jiǎn)介: 提供一個(gè)讓有限的窗口變成一個(gè)大數(shù)據(jù)集的靈活視圖。 術(shù)語(yǔ)表: Adapter:RecyclerView的子類...
    酷泡泡閱讀 5,358評(píng)論 0 16
  • 這篇文章分三個(gè)部分,簡(jiǎn)單跟大家講一下 RecyclerView 的常用方法與奇葩用法;工作原理與ListView比...
    LucasAdam閱讀 4,687評(píng)論 0 27
  • 男人要么穿上軍裝保家衛(wèi)國(guó),要么穿上西裝運(yùn)籌帷幄最近看戰(zhàn)狼二,看的是激動(dòng)不已,今天也趁熱打鐵總結(jié)下recyclerV...
    往事一塊六毛八閱讀 547評(píng)論 0 1
  • 和那些逆流而上的魚們比 安安逸逸地在小溪里 成群結(jié)隊(duì)地嬉戲 在清澈水里歡暢的草魚 顯得那么不思上進(jìn) 不要問魚 你還...
    叫我梅芳就好閱讀 254評(píng)論 0 0

友情鏈接更多精彩內(nèi)容