RecyclerView的滚动事件OnScrollListener研究
(1)滚动事件分类
列表的滚动一般分为两种:
1.手指按下 -> 手指拖拽列表移动 -> 手指停止拖拽 -> 抬起手指
2.手指按下 -> 手指快速拖拽后抬起手指 -> 列表继续滚动 -> 停止滚动
上面的过程的状态变化如下:
1.静止 -> 被迫拖拽移动 -> 静止
2.静止 -> 被迫拖拽移动 -> 自己滚动 -> 静止
(2)监听RecyclerView的滚动
有两种方式可以监听滚动事件:
1.setOnScrollListener(OnScrollListener listener)
2.addOnScrollListener(OnScrollListener listener)
其中 setOnScrollListener 由于可能出现空指针的风险,已经过时。建议用addOnScrollListener。
(3)OnScrollListener
/**
* An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event
* has occurred on that RecyclerView.
* <p>
* @see RecyclerView#addOnScrollListener(OnScrollListener)
* @see RecyclerView#clearOnChildAttachStateChangeListeners()
*
*/
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){}
}
OnScrollListener类是个抽象类,有两个方法:
void onScrollStateChanged(RecyclerView recyclerView, int newState): 滚动状态变化时回调
void onScrolled(RecyclerView recyclerView, int dx, int dy): 滚动时回调
3.1 onScrollStateChanged(RecyclerView recyclerView, int newState)方法
回调的两个变量的含义:
recyclerView: 当前在滚动的RecyclerView
newState: 当前滚动状态.
其中newState有三种值:
/**
* 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;
3.2 onScrolled(RecyclerView recyclerView, int dx, int dy)方法
回调的三个变量含义:
recyclerView : 当前滚动的view
dx : 水平滚动距离
dy : 垂直滚动距离
dx > 0 时为手指向左滚动,列表滚动显示右面的内容
dx < 0 时为手指向右滚动,列表滚动显示左面的内容
dy > 0 时为手指向上滚动,列表滚动显示下面的内容
dy < 0 时为手指向下滚动,列表滚动显示上面的内容
(4)canScrollVertically和canScrollHorizontally方法
public boolean canScrollVertically (int direction)
这个方法是判断View在竖直方向是否还能向上,向下滑动。
其中,direction为 -1 表示手指向下滑动(屏幕向上滑动), 1 表示手指向上滑动(屏幕向下滑动)。
public boolean canScrollHorizontally (int direction)
这个方法用来判断 水平方向的滑动
例如:
RecyclerView.canScrollVertically(1)的值表示是否能向下滚动,false表示已经滚动到底部
RecyclerView.canScrollVertically(-1)的值表示是否能向上滚动,false表示已经滚动到顶部
(5)两种判断是否到底部的方法:
5.1方法一:
如果 当前
第一个可见item的位置 + 当前可见的item个数 >= item的总个数
这样就可以判断出来,是在底部了。
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
来判断是否是底部。
5.2方法二:
通过canScrollVertically 来判断
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();
// }
// }
}
};
参考:
http://blog.devwiki.net/index.php/2016/06/13/RecyclerView-Scroll-Listener.html
RecyclerView的滚动事件OnScrollListener研究的更多相关文章
- RecyclerView的滚动事件分析
列表的滚动一般分为两种: 手指按下 -> 手指拖拽列表移动 -> 手指停止拖拽 -> 抬起手指 手指按下 -> 手指快速拖拽后抬起手指 -> 列表继续滚动 -> 停 ...
- 在RecyclerView列表滚动的时候显示或者隐藏Toolbar
先看一下效果: 本文将讲解如何实现类似于Google+应用中,当列表滚动的时候,ToolBar(以及悬浮操作按钮)的显示与隐藏(向下滚动隐藏,向上滚动显示),这种效果在Material Design ...
- Vue 爬坑之路(七)—— 监听滚动事件 实现动态锚点
前几天做项目的时候,需要实现一个动态锚点的效果 如果是传统项目,这个效果就非常简单.但是放到 Vue 中,就有两大难题: 1. 在没有 jQuery 的 animate() 方法的情况下,如何实现平滑 ...
- listView滚动事件
listView滚动事件 实现接口: android.widget.AbsListView.OnScrollListener Interface definition for a callback t ...
- 死磕到底RecyclerView | RecyclerView 的滚动是怎么实现的?
RecyclerView 是一个展示列表的控件,其中的子控件可以被滚动.这是怎么实现的?以走查源码的方式一探究竟. 切入点:滚动事件 阅读源码时,如何在浩瀚的源码中选择合适的切入点很重要,选好了能少走 ...
- js鼠标滑轮滚动事件绑定(兼容主流浏览器)
/** Event handler for mouse wheel event. *鼠标滚动事件 */ var wheel = function(event) { var delta = 0; if ...
- 鼠标滚动事件兼容性 wheel、onwheel
wheelEvent = "onwheel" in document.createElement("div") ? "wheel" : // ...
- JavaScript----分层导航 滚动事件
分层导航 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- js网页滚动条滚动事件实例分析
本文实例讲述了js网页滚动条滚动事件用法.分享给大家供大家参考.具体分析如下: 在做js返回顶部的效果时,要监听网页滚动条滚动事件,这个事件就是:window.onscroll.当onscroll事件 ...
随机推荐
- git 本地与服务器的同步
如果在服务器上直接用 git init 则会自动在当前目录创建.git目录但在本地clone后修改后,不能push,这是由于git默认拒绝了push操作,需要进行设置,修改.git/config添加如 ...
- 解决input框中加入disabled="disabled"之后,改变字体的颜色(默认的是灰色)
在input框中加入disabled="disabled"之后,字体默认的就变成灰色了 解决方案 input[disabled]{color:#fff;opacity:1} dis ...
- linux:C++实现ping
转载自: //http://blog.chinaunix.net/uid-25909722-id-2762079.html #include <stdio.h> #include < ...
- mysql 表分区 查看表分区 修改表分区
原文地址:http://blog.csdn.net/feihong247/article/details/7885199 一. mysql分区简介 数据库分区 数据库分区是一种物理数据库设 ...
- 使用Chrome保存网页为mht文件
一直在使用chrome浏览器,但由于需要将部分网页保存为mht文件,却发现chrome默认不支持.chrome浏览器默认支持2种:单独的html文件,全部: 万能的chrome怎么可能保存不了mht? ...
- Selenium2(WebDriver)总结(二)---Firefox的firebug插件参数设置(补充)
本文是对上一节的补充:http://www.cnblogs.com/puresoul/p/4251536.html 使用Selenium2(webdriver)启动firefox且自动加载firebu ...
- VMware vSphere can virtualize itself + 64-bit nested guests
Running VMware ESXi inside a virtual machine is a great way to experiment with different configurati ...
- XMLHttpRequest HTTP请求的返回码为0 http status = 0
由于用户在http返回前关闭/取消,或者防火墙等原因,会造成http请求没有status信息.w3c有相应的说明:http://www.w3.org/TR/XMLHttpRequest/#the-st ...
- ORA-01940: 无法删除当前连接的用户
删除用户报错 SQL> drop user ODI_SRC CASCADE; drop user ODI_SRC CASCADE * 第 1 行出现错误: ORA: 无法删除当前连接的用户 查看 ...
- cgroup 分析之CPU和内存部分
https://ggaaooppeenngg.github.io/zh-CN/2017/05/07/cgroups-%E5%88%86%E6%9E%90%E4%B9%8B%E5%86%85%E5%AD ...