美文网首页
RecyclerView滚动监听详解

RecyclerView滚动监听详解

作者: CarlosLynn | 来源:发表于2021-04-12 10:49 被阅读0次

OnScrollListener

RecyclerView的滚动监回调到抽象类.

/**
 * An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event
 * has occurred on that RecyclerView.
  *当RecyclerView上有滚动事件触发的时候可以添加OnScrollListener用于接收消息事件
  * 已发生在该回收器视图上。
 * <p>
 * @see RecyclerView#addOnScrollListener(OnScrollListener)
 *            RecyclerView中添加滚动监听的函数.
 * @see RecyclerView#clearOnChildAttachStateChangeListeners()
 *            RecyclerView中移除滚动监听的函数.
 */
public abstract static class OnScrollListener {
    /**
     * Callback method to be invoked when RecyclerView's scroll state changes.
     *  当RecyclerView的滚动状态发生变化的时候回调此函数.
     *
     * @param recyclerView The RecyclerView whose scroll state has changed.
     *                滚动状态变化的recyclerView实体对象.
     * @param newState     The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
     *                     {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
     *                最新的状态.
     */
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState){}

    /**
     * Callback method to be invoked when the RecyclerView has been scrolled. This will be
     * called after the scroll has completed.
     * 当RecyclerView的滚动的时候回调此函数.在滚动完成后回调.
     * <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.
     *  如果在一个布局计算后可见条目范围变化了此回调同时也会进行回调,在这种情况下,dx和dy将为0。
     *
     * @param recyclerView The RecyclerView which scrolled.
     *  滚动的RecyclerView 实体对象
     * @param dx The amount of horizontal scroll.
     *                     水平方向上滚动偏移量.
     * @param dy The amount of vertical scroll.垂直方向上滚动的偏移量,正值表示往下偏移,负值为向上偏移.
     */
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy){}
}

RecyclerView添加的回调监听

/**
 * Add a listener that will be notified of any changes in scroll state or position.
 *
 * <p>Components that add a listener should take care to remove it when finished.
 * Other components that take ownership of a view may call {@link #clearOnScrollListeners()}
 * to remove all attached listeners.</p>
 *
 * @param listener listener to set
 */
public void addOnScrollListener(@NonNull OnScrollListener listener) {
    if (mScrollListeners == null) {
        mScrollListeners = new ArrayList<>();
    }
    mScrollListeners.add(listener);
}

调用

recyclerView.run {
    layoutManager = GridLayoutManager(mContext, 3)
    adapter = mAdapter
    addOnScrollListener(object : RecyclerView.OnScrollListener() {

        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
            if (RecyclerView.SCROLL_STATE_IDLE == newState) {
                val firstIndex: Int =
                    (layoutManager as GridLayoutManager).findFirstVisibleItemPosition()
                Log.i(TAG, "onScrollStateChanged: $firstIndex")
            }
        }

        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            Log.i(TAG, "onScrolled: (dx:$dx,dx:$dy)")
        }
    })
}

1,手指向上滑动一段距离

2021-04-12 10:26:33.413 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:60)
2021-04-12 10:26:33.431 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:109)
2021-04-12 10:26:33.446 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:231)
2021-04-12 10:26:33.450 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:21)
2021-04-12 10:26:33.459 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:156)
2021-04-12 10:26:33.472 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:144)
2021-04-12 10:26:33.480 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:139)
2021-04-12 10:26:33.492 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:136)
2021-04-12 10:26:33.498 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:131)
2021-04-12 10:26:33.513 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:127)
2021-04-12 10:26:33.533 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:123)
2021-04-12 10:26:33.545 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:120)
2021-04-12 10:26:33.562 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:116)
2021-04-12 10:26:33.579 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:112)
2021-04-12 10:26:33.595 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:109)
2021-04-12 10:26:33.611 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:106)

2,手指向下滑动一段距离

2021-04-12 10:28:06.114 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:-17)
2021-04-12 10:28:06.124 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:-20)
2021-04-12 10:28:06.134 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:-27)
2021-04-12 10:28:06.151 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:-52)
2021-04-12 10:28:06.167 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:-66)
2021-04-12 10:28:06.192 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:-117)
2021-04-12 10:28:06.209 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:-62)
2021-04-12 10:28:06.224 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:-68)
2021-04-12 10:28:06.240 20578-20578/com.lanto.water I/DrinkChooseActivity: onScrolled: (dx:0,dx:-49)

相关文章

网友评论

      本文标题:RecyclerView滚动监听详解

      本文链接:https://www.haomeiwen.com/subject/upbtlltx.html