推荐阅读:使用recyclerview添加header和footer的方法
recyclerview 是android l版本中新添加的一个用来取代listview的sdk,它的灵活性与可替代性比listview更好。本文给大家介绍如何为recyclerview添加下拉刷新和上拉加载,过去在listview当中添加下拉刷新和上拉加载是非常方便的利用addheaderview和addfooterview,recyclerview的刷新同样也是需要靠list当中添加head或foot来实现的。本篇不会详细告诉你怎么去做一个下拉刷新和加载的效果,而是如何快速利用现有的知名开源库来为recyclerview实现下面的效果。
先给大家展示下效果图,感兴趣的朋友请继续阅读全文。
这个下拉刷新不是靠list当中添加headerview来实现的,而是在recyclerview外面套了一层viewgroup而这个viewgroup支持下拉刷新,可以灵巧的实现一些子view的下拉刷新,这个就是大名鼎鼎的android-ultra-pull-to-refresh开源库,作者liaohuqiu,但是很遗憾它不支持上拉加载,本篇用的是在这个库基础上进行了拓展并实现了上拉加载效果的开源库,上拉的效果实现是靠recyclerview里添加footerview,github地址:https://github.com/chanven/commonpulltorefresh 感谢活跃在开源社区的开发者们。
具体的实现步骤:(开发工具:android studio)
1:gradle配置 build.gradle
1
2
3
4
5
|
//可选 或者在上面地址中下载library源代码copy到项目里面 compile 'com.chanven.lib:cptr:1.0.0' // recyclerview和cardview compile 'com.android.support:recyclerview-v7:23.1.1' compile 'com.android.support:cardview-v7:23.1.1' |
2:主布局文件 activity_main.xml 里面一个recyclerview 外面嵌套的是支持下拉刷新的viewgroup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" > <com.chanven.lib.cptr.ptrclassicframelayout android:id= "@+id/rotate_header_list_view_frame" xmlns:cube_ptr= "http://schemas.android.com/apk/res-auto" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "#e1e1e1" cube_ptr:ptr_duration_to_close= "200" cube_ptr:ptr_duration_to_close_header= "1000" cube_ptr:ptr_keep_header_when_refresh= "true" cube_ptr:ptr_pull_to_fresh= "false" cube_ptr:ptr_ratio_of_header_height_to_refresh= "1.2" cube_ptr:ptr_resistance= "1.7" > <android.support.v7.widget.recyclerview android:background= "#ffffff" xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:id= "@+id/rv_list" /> </com.chanven.lib.cptr.ptrclassicframelayout> </linearlayout> |
3:列表项布局 rv_item.xml 外面一个cardview的卡片式容器,里面一个textview
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "utf-8" ?> <android.support.v7.widget.cardview xmlns:card_view= "http://schemas.android.com/apk/res-auto" xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_margin= "8dp" android:id= "@+id/cv_item" android:foreground= "?android:attr/selectableitembackground" card_view:cardcornerradius= "4dp" card_view:cardelevation= "4dp" > <linearlayout android:layout_width= "match_parent" android:layout_height= "wrap_content" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/tv_item_text" android:text= "test" android:layout_margin= "8dp" /> </linearlayout> </android.support.v7.widget.cardview> |
4:recyclerview的adapter rvadapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public class rvadapter extends recyclerview.adapter<recyclerview.viewholder> { private layoutinflater mlayoutinflater; private context mcontext; private list <string> mtitle; public rvadapter(context context,list<string> title){ mcontext=context; mtitle=title; mlayoutinflater=layoutinflater.from(context); } //自定义的viewholder,持有每个item的的所有界面元素 public static class normalviewholder extends recyclerview.viewholder { textview mtextview; cardview mcardview; public normalviewholder(view itemview) { super (itemview); mtextview=(textview)itemview.findviewbyid(r.id.tv_item_text); mcardview=(cardview)itemview.findviewbyid(r.id.cv_item); } } //在该方法中我们创建一个viewholder并返回,viewholder必须有一个带有view的构造函数,这个view就是我们item的根布局,在这里我们使用自定义item的布局; @override public normalviewholder oncreateviewholder(viewgroup parent, int viewtype) { return new normalviewholder(mlayoutinflater.inflate(r.layout.rv_item,parent, false )); } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { normalviewholder viewholder = (normalviewholder) holder; viewholder.mtextview.settext(mtitle.get(position)); } //获取数据的数量 @override public int getitemcount() { return mtitle== null ? 0 : mtitle.size(); } } |
5:最重要的activity的实现,mainactivity.java,代码含义已经注释
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
public class mainactivity extends appcompatactivity { private recyclerview mrecyclerview; //支持下拉刷新的viewgroup private ptrclassicframelayout mptrframe; //list数据 private list<string> title = new arraylist<>(); //recyclerview自定义adapter private rvadapter adapter; //添加header和footer的封装类 private recycleradapterwithhf madapter; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mrecyclerview = (recyclerview) findviewbyid(r.id.rv_list); linearlayoutmanager layoutmanager = new linearlayoutmanager( this ); layoutmanager.setorientation(linearlayoutmanager.vertical); mrecyclerview.setlayoutmanager(layoutmanager); adapter = new rvadapter(mainactivity. this , title); madapter = new recycleradapterwithhf(adapter); mrecyclerview.setadapter(madapter); mptrframe = (ptrclassicframelayout) findviewbyid(r.id.rotate_header_list_view_frame); //下拉刷新支持时间 mptrframe.setlastupdatetimerelateobject( this ); //下拉刷新一些设置 详情参考文档 mptrframe.setresistance( 1 .7f); mptrframe.setratioofheaderheighttorefresh( 1 .2f); mptrframe.setdurationtoclose( 200 ); mptrframe.setdurationtocloseheader( 1000 ); // default is false mptrframe.setpulltorefresh( false ); // default is true mptrframe.setkeepheaderwhenrefresh( true ); //进入activity就进行自动下拉刷新 mptrframe.postdelayed( new runnable() { @override public void run() { mptrframe.autorefresh(); } }, 100 ); //下拉刷新 mptrframe.setptrhandler( new ptrdefaulthandler() { @override public void onrefreshbegin(ptrframelayout frame) { title.clear(); //模拟数据 for ( int i = 0 ; i <= 5 ; i++) { title.add(string.valueof(i)); } //模拟联网 延迟更新列表 new handler().postdelayed( new runnable() { public void run() { madapter.notifydatasetchanged(); mptrframe.refreshcomplete(); mptrframe.setloadmoreenable( true ); } }, 1000 ); } }); //上拉加载 mptrframe.setonloadmorelistener( new onloadmorelistener() { @override public void loadmore() { //模拟联网延迟更新数据 new handler().postdelayed( new runnable() { @override public void run() { //模拟数据 for ( int i = 0 ; i <= 5 ; i++) { title.add(string.valueof(i)); } madapter.notifydatasetchanged(); mptrframe.loadmorecomplete( true ); toast.maketext(mainactivity. this , "load more complete" , toast.length_short) .show(); } }, 1000 ); } }); } } |
以上内容是小编给大家介绍的android中使用recyclerview实现下拉刷新和上拉加载教程,希望对大家有所帮助!