android recyclerview判断左右滑动
发布网友
发布时间:2022-04-22 11:39
我来回答
共1个回答
热心网友
时间:2023-06-23 10:07
RecycleView设置滑动监听:
mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
判断滑动方向:
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {//下滑动作
}
if (dy < 0) {//上滑动作
}
super.onScrolled(recyclerView, dx, dy);
}
判断是否滑动到顶部、底部:
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
if(newState == RecyclerView.SCROLL_STATE_IDLE){//停止滑动
if(recyclerView.canScrollVertically(1)){
Toast.show("滑动到顶部");
}
if(recyclerView.canScrollVertically(-1)){
Toast.show("滑动到底部");
}
}
super.onScrollStateChanged(recyclerView, newState);
}
recyclerView.canScrollVertically(1); false表示不能往上滑动,即代表到顶部了;
recyclerView.canScrollVertically(-1); false表示不能往下滑动,即代表到底部了;