列表滚动
列表滚动有两种场景:
1.拖拽滚动:手指按下>>>手指拖拽列表滚动>>>手指停止拖拽>>>手指抬起
2.快速滑动:手指按下>>>手指快速拖拽后手指抬起>>>列表继续滑动>>>停止滑动
滚动状态:静止 和 滚动(被动拖拽移动和惯性滚动)
监听RecyclerView的滚动
如何监听RecyclerView的滚动,直接查看源码是实践方法。
源码
添加监听接口
翻阅RecyclerView的源码,可以看到以下代码:
/**
* Set a listener that will be notified of any changes in scroll state or position.
*
* @param listener Listener to set or null to clear
*
* @deprecated Use {@link #addOnScrollListener(OnScrollListener)} and
* {@link #removeOnScrollListener(OnScrollListener)}
*/
@Deprecated
public void setOnScrollListener(OnScrollListener listener) {
mScrollListener = listener;
}
/**
* 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 or null to clear
*/
public void addOnScrollListener(OnScrollListener listener) {
if (mScrollListeners == null) {
mScrollListeners = new ArrayList<>();
}
mScrollListeners.add(listener);
}
有两种添加滚动监听:
1.setOnScrollListener()
2.addOnScrollListener()
源码中我们可以看到setOnScrollListener被标记为@Deprecated表示该方法已经过时
接口介绍
设置监听的抽象类OnScrollListener,源码如下
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){}
}
滚动过程中,监听器会回调这两个方法:
onScrollStateChanged:滚动状态变化时回调
onScrolled: 滚动时回调
区别:滚动状态变化监听、滚动过程中监听
onScrollStateChanged 方法
在 void onScrollStateChanged(RecyclerView recyclerView, int newState)中两个参数:
1. recyclerView: 滚动对象;
2. newState:滚动状态;
newState有三种值,分别代表滚动状态:
//空闲状态
public static final int SCROLL_STATE_IDLE = 0;
//滑动状态(正在被外部拖拽,一般为用户正在用手指滚动)
public static final int SCROLL_STATE_DRAGGING = 1;
//滑动后自然沉降的状态(自动滚动开始)
public static final int SCROLL_STATE_SETTLING = 2;
onScrolled 方法
在void onScrolled(RecyclerView recyclerView, int dx, int dy)方法中三个变量:
1.recyclerView:滚动控件对象
2.dx:水平滚动距离
3.dy:垂直滚动距离
其中dy:
dy > 0 时为向上滚动
dy < 0 时为向下滚动
滚动过程
在滚动中会不断的调取onScrollStateChanged和onScrolled 两个方法。
1.其中滚动状态变化:
缓慢拖拽回调过程:
SCROLL_STATE_DRAGGING 多次
SCROLL_STATE_IDLE
快速滚动回调过程:
SCROLL_STATE_DRAGGING 多次
SCROLL_STATE_SETTLING 1次
SCROLL_STATE_IDLE 1次
2.滑动到顶部和底部
RecyclerView.canScrollVertically(1)的值表示是否滚动到底部
RecyclerView.canScrollVertically(-1)的值表示是否滚动到顶部
http://blog.devwiki.net/index.php/2016/06/13/RecyclerView-Scroll-Listener.html