Android 中RecyclerView顶部刷新实现详解
作者:ccpat 时间:2022-08-20 17:22:39
Android 中RecyclerView顶部刷新实现详解
1. RecyclerView顶部刷新的原理
RecyclerView顶部刷新的实现通常都是在RecyclerView外部再包裹一层布局。在这个外层布局中,还包含一个自定义的View,作为顶部刷新时的指示View。也就是说,外层布局中包含两个child,一个顶部刷新View,一个RecyclerView,顶部刷新View默认是隐藏不可见的。在外层布局中对滑动事件进行处理,当RecyclerView滑动到顶部并继续下滑的时候,根据滑动的距离决定顶部刷新View的显示。当滑动距离超过某个设定的值的时候,执行顶部刷新操作。
2. RecyclerView顶部刷新的实现
RecyclerView顶部刷新的实现一般包含如下步骤。
创建自定义的布局类,它可以继承自已有的布局类,如LinearLayout,也可以直接继承自ViewGroup。
添加RecyclerView和顶部刷新View作为其child。
重写自定义的布局类的onMeasure(),onLayout(),dispatchTouchEvent(),onInterceptTouchEvent()等方法。
步骤3是其中最复杂的部分,需要在这些重写的方法中,完成自身和child的测量,布局和滑动事件的处理。尤其是滑动事件的处理,需要对Android View的滑动机制有全面的了解才能实现。
Google在19.1之后的support library v4包中增加了SwipeRefreshLayout类。它继承自ViewGroup,在它的内部包含了一个CircleImageView对象作为顶部刷新View,同时它实现了上述步骤3的全部功能。将SwipeRefreshLayout和RecyclerView结合在一起,可以轻松的实现顶部刷新功能。
3.1 SwipeRefreshLayout用法
在介绍SwipeRefreshLayout和RecyclerView结合实现顶部刷新功能之前,先介绍下SwipeRefreshLayout的用法。
SwipeRefreshLayout最重要的两个方法是:setOnRefreshListener()和setRefreshing()。
setOnRefreshListener()方法用来设置顶部刷新事件的监听,当需要执行顶部刷新时会调用此listener的onRefresh()方法,来获取最新的数据。
setRefreshing()方法用来设置顶部刷新状态。当数据获取完成后,需要调用此方法表示刷新完成。
除此之外,SwipeRefreshLayout还提供了一些方法用来设置顶部刷新View进度条颜色,背景色等。
3.2 SwipeRefreshLayout结合RecyclerView实现顶部刷新
SwipeRefreshLayout结合RecyclerView实现顶部刷新功能非常简单,只需要在SwipeRefreshLayout中包含一个RecyclerView作为其child即可。可以直接通过XML文件来布局。
XML布局如下。
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
为了方便使用,可以对这里的布局设置通过代码进行封装,创建一个自定义的XSwipeRefreshLayout类来实现。代码方式实现如下。由于布局非常简单,代码中就没有引入布局文件了。
public class XSwipeRefreshLayout extends SwipeRefreshLayout {
private RecyclerView mRecyclerView;
public XSwipeRefreshLayout(Context context) {
super(context);
init(context);
}
public XSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mRecyclerView = new RecyclerView(context);
addView(mRecyclerView);
}
}
3.3 操作RecyclerView
对XML方式实现的顶部刷新,要操作RecyclerView只需要通过findViewById()找到对应的RecyclerView对象,然后调用相应的方法即可。
对代码方式实现的顶部刷新,需要在XSwipeRefreshLayout中增加操作内部RecyclerView的接口。可以有两种方式:一种是在XSwipeRefreshLayout中增加getRecyclerView()方法,返回内部的RecyclerView对象,然后在外部调用RecyclerView对象的方法。另一种是XSwipeRefreshLayout中增加RecyclerView对应的各种方法,然后透传给内部的RecyclerView对象。这两种方式的示例代码如下。
public class XSwipeRefreshLayout extends SwipeRefreshLayout {
private RecyclerView mRecyclerView;
public XSwipeRefreshLayout(Context context) {
super(context);
init(context);
}
public XSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mRecyclerView = new RecyclerView(context);
addView(mRecyclerView);
}
public RecyclerView getRecyclerView() {
return mRecyclerView;
}
}
public class XSwipeRefreshLayout extends SwipeRefreshLayout {
private RecyclerView mRecyclerView;
public XSwipeRefreshLayout(Context context) {
super(context);
init(context);
}
public XSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mRecyclerView = new RecyclerView(context);
addView(mRecyclerView);
}
public RecyclerView.Adapter getAdapter() {
return mRecyclerView.getAdapter();
}
public void setAdapter(RecyclerView.Adapter adapter) {
mRecyclerView.setAdapter(adapter);
}
public void setLayoutManager(RecyclerView.LayoutManager layout) {
mRecyclerView.setLayoutManager(layout);
}
// 将需要用到的每个RecyclerView的方法都写在这里
.....
}
3. RecyclerView同时支持顶部刷新和底部刷新
在实际的应用中,顶部刷新通常都需要和底部刷新一起使用。要让RecyclerView同时支持顶部刷新和底部刷新,只需要将上述顶部刷新实现中的RecyclerView换成上一篇文章中XRecyclerView即可。
XML布局如下。
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<cnx.ccpat.testapp.XRecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</cnx.ccpat.testapp.XRecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
对应的代码方式实现如下。
public class XSwipeRefreshLayout extends SwipeRefreshLayout {
private XRecyclerView mRecyclerView;
public XSwipeRefreshLayout(Context context) {
super(context);
init(context);
}
public XSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mRecyclerView = new XRecyclerView(context);
addView(mRecyclerView);
}
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/ccpat/article/details/73912296
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring boot的上传图片功能实例详解
c# 配置文件App.config操作类库的方法
解决RestTemplate 请求url中包含百分号 会被转义成25的问题
android中在Activity中响应ListView内部按钮的点击事件的两种方法
Java中缀表达式转后缀表达式实现方法详解
![](https://img.aspxhome.com/file/2023/5/104005_0s.png)
Java包装类的概述与应用
![](https://img.aspxhome.com/file/2023/0/91110_0s.png)
SpringBoot通过自定义注解实现参数校验
![](https://img.aspxhome.com/file/2023/3/58343_0s.png)
Java简单工厂模式定义与用法实例分析
![](https://img.aspxhome.com/file/2023/0/79160_0s.png)
教你如何监控 Java 线程池运行状态的操作(必看)
![](https://img.aspxhome.com/file/2023/9/63079_0s.jpg)
解决C#中取消方向键对控件焦点控制的实现方法
java将一个目录下的所有数据复制到另一个目录下
详解Spring中使用@within与@target的区别
![](https://img.aspxhome.com/file/2023/0/84700_0s.jpg)
springboot从application.properties中注入list, map方式
![](https://img.aspxhome.com/file/2023/9/60539_0s.jpg)
详细聊聊SpringBoot中动态切换数据源的方法
![](https://img.aspxhome.com/file/2023/9/59879_0s.png)
C# List<T>的用法小结
Android WebView的使用方法总结
Android使用android-wheel实现省市县三级联动
![](https://img.aspxhome.com/file/2023/2/129022_0s.gif)
C#获取汉字字符串拼音首字母的方法
c++野指针的原理以及避免方法
解决Eclipse创建android项目无法正常预览布局文件问题的方法
![](https://img.aspxhome.com/file/2023/6/82316_0s.png)