欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

SwipeRefreshLayout+RecyclerView实现上拉刷新和下拉刷新功能

程序员文章站 2022-04-12 11:33:40
swiperefreshlayout 是谷歌公司推出的用于下拉刷新的控件,swiperefreshlayout已经被放到了sdk中,在version 19.1之后swipe...

swiperefreshlayout 是谷歌公司推出的用于下拉刷新的控件,swiperefreshlayout已经被放到了sdk中,在version 19.1之后swiperefreshlayout 被放到support v4中。

源码在sdk\sdk\extras\android\support\v4\src\java\android\support\v4\widget\swiperefreshlayout.java

谷歌公司只提供了下拉刷新的功能,recyclerview的出现基本就是为了替代listview,gridview的。

今天说一下最常见的下拉刷新  和 上拉刷新的功能。

布局文件:

<android.support.v4.widget.swiperefreshlayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/swipe_refresh_widget"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >
 
 <android.support.v7.widget.recyclerview
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:cachecolorhint="@null"
  android:scrollbars="vertical" />
 
</android.support.v4.widget.swiperefreshlayout>

在activity中引用这个布局并初始化

@override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 
 mswiperefreshwidget = (swiperefreshlayout) findviewbyid(r.id.swipe_refresh_widget);
 mrecyclerview = (recyclerview) findviewbyid(android.r.id.list);
 
 mswiperefreshwidget.setcolorscheme(r.color.color1, r.color.color2,
 r.color.color3, r.color.color4);
 mswiperefreshwidget.setonrefreshlistener(this);
 
 // 这句话是为了,第一次进入页面的时候显示加载进度条
 mswiperefreshwidget.setprogressviewoffset(false, 0, (int) typedvalue
 .applydimension(typedvalue.complex_unit_dip, 24, getresources()
  .getdisplaymetrics()));
 
 mrecyclerview.setonscrolllistener(new recyclerview.onscrolllistener() {
 
 @override
 public void onscrollstatechanged(recyclerview recyclerview,
  int newstate) {
 super.onscrollstatechanged(recyclerview, newstate);
 if (newstate == recyclerview.scroll_state_idle
  && lastvisibleitem + 1 == adapter.getitemcount()) {
  mswiperefreshwidget.setrefreshing(true);
  // 此处在现实项目中,请换成网络请求数据代码,sendrequest .....
  handler.sendemptymessagedelayed(0, 3000);
 }
 }
 
 @override
 public void onscrolled(recyclerview recyclerview, int dx, int dy) {
 super.onscrolled(recyclerview, dx, dy);
 lastvisibleitem = mlayoutmanager.findlastvisibleitemposition();
 }
 
 });
 
 mrecyclerview.sethasfixedsize(true);
 mlayoutmanager = new linearlayoutmanager(this);
 mrecyclerview.setlayoutmanager(mlayoutmanager);
 mrecyclerview.setitemanimator(new defaultitemanimator());
 
 adapter = new sampleadapter();
 mrecyclerview.setadapter(adapter);
 
 // 此处在现实项目中,请换成网络请求数据代码,sendrequest .....
 handler.sendemptymessagedelayed(0, 3000);
 }

swiperefreshlayout里面需要注意的api:

1、setonrefreshlistener(onrefreshlistener listener)  设置下拉监听,当用户下拉的时候会去执行回调
2、setcolorschemecolors(int... colors) 设置 进度条的颜色变化,最多可以设置4种颜色
3、setprogressviewoffset(boolean scale, int start, int end) 调整进度条距离屏幕顶部的距离
4、setrefreshing(boolean refreshing) 设置swiperefreshlayout当前是否处于刷新状态,一般是在请求数据的时候设置为true,在数据被加载到view中后,设置为false。

recyclerview的实现:

第一种,下拉刷新和上拉刷新都用swiperefreshlayout 自带的进度条

mrecyclerview = (recyclerview) findviewbyid(android.r.id.list);
mrecyclerview.setonscrolllistener(new recyclerview.onscrolllistener() {
 
 @override
 public void onscrollstatechanged(recyclerview recyclerview,
  int newstate) {
 super.onscrollstatechanged(recyclerview, newstate);
 if (newstate == recyclerview.scroll_state_idle
  && lastvisibleitem + 1 == adapter.getitemcount()) {
  mswiperefreshwidget.setrefreshing(true);
  // 此处在现实项目中,请换成网络请求数据代码,sendrequest .....
  handler.sendemptymessagedelayed(0, 3000);
 }
 }
 
 @override
 public void onscrolled(recyclerview recyclerview, int dx, int dy) {
 super.onscrolled(recyclerview, dx, dy);
 lastvisibleitem = mlayoutmanager.findlastvisibleitemposition();
 }
 });
 
 mrecyclerview.sethasfixedsize(true);
 mlayoutmanager = new linearlayoutmanager(this);
 mrecyclerview.setlayoutmanager(mlayoutmanager);
 mrecyclerview.setitemanimator(new defaultitemanimator());
 
 adapter = new sampleadapter();
 mrecyclerview.setadapter(adapter);

第二种实现下拉刷新用swiperefreshlayout 自带的进度条, 上拉刷新用类似listview的刷新 提示“加载中”等信息。

我们可以给recyclerview 也添加一个类似footerview的item。
我们在adapter中实现:

public class sampleadapter extends recyclerview.adapter<viewholder> {
 private list<integer> list;
 
 private static final int type_item = 0;
 private static final int type_footer = 1;
 
 public list<integer> getlist() {
 return list;
 }
 
 public sampleadapter() {
 list = new arraylist<integer>();
 }
 
 // recyclerview的count设置为数据总条数+ 1(footerview)
 @override
 public int getitemcount() {
 return list.size() + 1;
 }
 
 @override
 public int getitemviewtype(int position) {
 // 最后一个item设置为footerview
 if (position + 1 == getitemcount()) {
 return type_footer;
 } else {
 return type_item;
 }
 }
 
 @override
 public void onbindviewholder(viewholder holder, final int position) {
 if (holder instanceof itemviewholder) {
 ((itemviewholder) holder).textview.settext(string.valueof(list
  .get(position)));
 }
 }
 
 @override
 public viewholder oncreateviewholder(viewgroup parent, int viewtype) {
 if (viewtype == type_item) {
 view view = layoutinflater.from(parent.getcontext()).inflate(
  r.layout.list_item_text, null);
 view.setlayoutparams(new layoutparams(layoutparams.match_parent,
  layoutparams.wrap_content));
 return new itemviewholder(view);
 }
 // type == type_footer 返回footerview
 else if (viewtype == type_footer) {
 view view = layoutinflater.from(parent.getcontext()).inflate(
  r.layout.footerview, null);
 view.setlayoutparams(new layoutparams(layoutparams.match_parent,
  layoutparams.wrap_content));
 return new footerviewholder(view);
 }
 
 return null;
 }
 
 class footerviewholder extends viewholder {
 
 public footerviewholder(view view) {
 super(view);
 }
 
 }
 
 class itemviewholder extends viewholder {
 textview textview;
 
 public itemviewholder(view view) {
 super(view);
 textview = (textview) view.findviewbyid(r.id.text);
 }
 }
}

这样我们就可以针对footerview的布局做一些处理了,比如提示“加载中,”,“已经全部加载”等信息。更加灵活一点。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。