1. 程式人生 > >Android RecyclerView的滑動監聽

Android RecyclerView的滑動監聽

1 RecyclerView 的滑動監聽

1.1 RecyclerView 設定滑動監聽

<span style="color:#000000"><code class="language-java">
mRecyclerView.setOnScrollListener(<span style="color:#000088">new</span> RecyclerView.OnScrollListener() {
    <span style="color:#9b859d">@Override</span>
    <span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">onScrollStateChanged</span>(RecyclerView recyclerView, <span style="color:#000088">int</span> newState) {
        <span style="color:#000088">super</span>.onScrollStateChanged(recyclerView, newState);
    }

    <span style="color:#9b859d">@Override</span>
    <span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">onScrolled</span>(RecyclerView recyclerView, <span style="color:#000088">int</span> dx, <span style="color:#000088">int</span> dy) {
        <span style="color:#000088">super</span>.onScrolled(recyclerView, dx, dy);

    }
});
</code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

1.2 滑動監聽回撥方法說明

1.3 判斷RecyclerView的滑動方向

  • onScrollStateChanged(RecyclerView recyclerView, int newState)這個方法在RecyclerView的滑動狀態改變時會呼叫

對應的引數 newState 有三種狀態 ,官方這樣描述

<span style="color:#000000"><code>    <span style="color:#880000">/**
     * The RecyclerView is not currently scrolling.
     *<span style="color:#4f4f4f"> @see</span> #getScrollState()
     */</span>
    <span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">final</span> <span style="color:#000088">int</span> SCROLL_STATE_IDLE = <span style="color:#006666">0</span>;

    <span style="color:#880000">/**
     * The RecyclerView is currently being dragged by outside input such as user touch input.
     *<span style="color:#4f4f4f"> @see</span> #getScrollState()
     */</span>
    <span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">final</span> <span style="color:#000088">int</span> SCROLL_STATE_DRAGGING = <span style="color:#006666">1</span>;

    <span style="color:#880000">/**
     * The RecyclerView is currently animating to a final position while not under
     * outside control.
     *<span style="color:#4f4f4f"> @see</span> #getScrollState()
     */</span>
    <span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">final</span> <span style="color:#000088">int</span> SCROLL_STATE_SETTLING = <span style="color:#006666">2</span>;
</code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

也就是說,newState=0時,RecyclerView在停止狀態中 
newState=1和newState=2時,RecyclerView在滑動狀態中 
不同的是,當由0—>1 ,2 時,RecyclerView由靜止狀態下變為滑動狀態,然後1–0 滑動狀態變為靜止(呼叫方法 mHomeDateRecyclerView.smoothScrollToPosition(currentPostion); 等這一類方法所觸發) , 2–>0 滑動狀態變為靜止狀態(左右滑動RecyclerView 動態的慢慢結束)

  • onScrolled(RecyclerView recyclerView, int dx, int dy ) 這個方法在RecyclerView開始滑動時實時回撥

引數dx dy分別是RecyclerView在滑動過程中,在x軸方向(橫向滑動)與Y軸方向(縱向滑動)的偏移量 
dx>0 向右滑動 
dx<0 向左滑動

dy<0 向上滑動 
dy>0 向下滑動

 


2 RecyclerView 的滑動滑動距離獲取

可以在RecyclerView的滑動監聽中呼叫此方法,以不斷的來獲取RecyclerView的滑動距離

<span style="color:#000000"><code class="language-java"><span style="color:#880000">//mMTrainLinearLayoutManager 是 Recyclerview對應的佈局管理者</span>
<span style="color:#000088">if</span> (mMTrainLinearLayoutManager != <span style="color:#000088">null</span>) {
       <span style="color:#880000">//獲取RecyclerView當前頂部顯示的第一個條目對應的索引    </span>
       <span style="color:#000088">int</span> position = mMTrainLinearLayoutManager.findFirstVisibleItemPosition();
       <span style="color:#880000">//根據索引來獲取對應的itemView </span>
       View firstVisiableChildView = mMTrainLinearLayoutManager.findViewByPosition(position);
       <span style="color:#880000">//獲取當前顯示條目的高度</span>
       <span style="color:#000088">int</span> itemHeight = firstVisiableChildView.getHeight();
       <span style="color:#880000">//獲取當前Recyclerview 偏移量</span>
       <span style="color:#000088">int</span> flag = (position) * itemHeight - firstVisiableChildView.getTop();

       LogUtils.d(<span style="color:#009900">"scroll "</span> + dx + <span style="color:#009900">"  "</span> + dy + <span style="color:#009900">" flag "</span> + flag);
   }</code></span>