RecyclerView的滚动事件

滚动事件分类

列表的滚动一般分为两种:

1.手指按下 -> 手指拖拽列表移动 -> 手指停止拖拽 -> 抬起手指

2.手指按下 -> 手指快速拖拽后抬起手指 -> 列表继续滚动 -> 停止滚动

上面的过程的状态变化如下:

1.静止 -> 被迫拖拽移动 -> 静止

2.静止 -> 被迫拖拽移动 -> 自己滚动 -> 静止

监听RecyclerView的滚动

两种方式:

1
2
3
setOnScrollListener(OnScrollListener listener) //被淘汰

addOnScrollListener(OnScrollListener listener)

其中 setOnScrollListener 由于可能出现空指针的风险,已经过时。建议用addOnScrollListener。

onScrollListener
1
2
3
4
5
6
7

public abstract static class OnScrollListener {

public void onScrollStateChanged(RecyclerView recyclerView, int newState){}

public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
}

OnScrollListener类是个抽象类,有两个方法:

1
2
void onScrollStateChanged(RecyclerView recyclerView, int newState): 滚动状态变化时回调
void onScrolled(RecyclerView recyclerView, int dx, int dy): 滚动时回调
onScrollStateChanged(RecyclerView recyclerView, int newState)

回调的两个变量的含义:
recyclerView: 当前在滚动的RecyclerView
newState: 当前滚动状态.

其中newState有三种值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* The RecyclerView is not currently scrolling.(静止没有滚动)
*/
public static final int SCROLL_STATE_IDLE = 0;

/**
* The RecyclerView is currently being dragged by outside input such as user touch input.
*(正在被外部拖拽,一般为用户正在用手指滚动)
*/
public static final int SCROLL_STATE_DRAGGING = 1;

/**
* The RecyclerView is currently animating to a final position while not under outside control.
*(自动滚动)
*/
public static final int SCROLL_STATE_SETTLING = 2;
onScrolled(RecyclerView recyclerView, int dx, int dy)方法

回调的三个变量含义:
recyclerView : 当前滚动的view
dx : 水平滚动距离
dy : 垂直滚动距离

dx > 0 时为手指向左滚动,列表滚动显示右面的内容
dx < 0 时为手指向右滚动,列表滚动显示左面的内容
dy > 0 时为手指向上滚动,列表滚动显示下面的内容
dy < 0 时为手指向下滚动,列表滚动显示上面的内容

canScrollVertically和canScrollHorizontally方法
1
2
3
4
5
6
7
public boolean canScrollVertically (int direction)
这个方法是判断View在竖直方向是否还能向上,向下滑动。
其中,direction为 -1 表示手指向下滑动(屏幕向上滑动), 1 表示手指向上滑动(屏幕向下滑动)。

public boolean canScrollHorizontally (int direction)
这个方法用来判断 水平方向的滑动
@param direction为 -1 表示手指向左滑动, 1 表示手指向右滑动

例如:
RecyclerView.canScrollVertically(1)的值表示是否能向下滚动,false表示已经滚动到底部
RecyclerView.canScrollVertically(-1)的值表示是否能向上滚动,false表示已经滚动到顶部

两种判断是否到底部的方法
方法一

如果 当前
第一个可见item的位置 + 当前可见的item个数 >= item的总个数
这样就可以判断出来,是在底部了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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) //向下滚动
{
int visibleItemCount = mLinearLayoutManager.getChildCount();
int totalItemCount = mLinearLayoutManager.getItemCount();
int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();

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

通过
visibleItemCount + pastVisiblesItems >= totalItemCount
来判断是否是底部。

方法二

通过canScrollVertically 来判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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) //向下滚动
// {
// int visibleItemCount = mLinearLayoutManager.getChildCount();
// int totalItemCount = mLinearLayoutManager.getItemCount();
// int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();
//
// if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
// loading = true;
// loadMoreDate();
// }
// }
}
};
0%