Android 中RecyclerView顶部刷新实现详解

所属分类: 软件编程 / Android 阅读数: 62
收藏 0 赞 0 分享

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);
  }
}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多