Android RecyclerView上拉加载和下拉刷新(基础版)
程序员文章站
2023-12-19 09:34:22
这里讲述的是用谷歌原生的swiperefreshlayout,进行刷新,以及利用recycleview的滚动事件,判断是否到最后一个item,进行加载更多,这里加载更多是在...
这里讲述的是用谷歌原生的swiperefreshlayout,进行刷新,以及利用recycleview的滚动事件,判断是否到最后一个item,进行加载更多,这里加载更多是在recycleview的适配器中使用不同item进行完成的。
这是activity的xml布局:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.widget.swiperefreshlayout android:id="@+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f0f0f0" android:cliptopadding="false" android:paddingbottom="16dp" android:paddingtop="16dp"/> </android.support.v4.widget.swiperefreshlayout> <com.rey.material.widget.progressview android:id="@+id/progress_loading_main" app:pv_autostart="true" app:pv_circular="true" app:pv_progressstyle="@style/material.drawable.circularprogress" app:pv_progressmode="indeterminate" android:layout_width="50dp" android:layout_height="50dp" android:visibility="gone" android:layout_centerinparent="true"/> </relativelayout>
接下来是对应activity中的代码:
import android.os.bundle; import android.os.handler; import android.support.annotation.nullable; import android.support.design.widget.snackbar; import android.support.v4.app.fragment; import android.support.v4.widget.swiperefreshlayout; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import com.retrofit.wangfei.viewpagertablayout.util.constance; import com.retrofit.wangfei.viewpagertablayout.r; import com.retrofit.wangfei.viewpagertablayout.adapter.myrecycleviewadapter; import com.rey.material.widget.progressview; import java.util.arraylist; import java.util.list; import butterknife.bind; import butterknife.butterknife; public class homefragment extends fragment { @bind(r.id.recyclerview) recyclerview recyclerview; @bind(r.id.swipe_refresh_layout) swiperefreshlayout swiperefreshlayout; @bind(r.id.progress_loading_main) progressview progress_loading_main; // 加载数据时显示的进度圆圈 private linearlayoutmanager mrecycleviewlayoutmanager; private int mpagenum = 1; private list<string> lists = new arraylist<>(); private myrecycleviewadapter madapter; public static homefragment newinstance() { homefragment fragment = new homefragment(); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_home, container, false); butterknife.bind(this, view); return view; } /**在oncreateview方法后执行*/ @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); madapter = new myrecycleviewadapter(lists,getactivity()); initrecyclerview(); swiperefreshlayout.setcolorschemeresources(constance.colors);//设置下拉刷新控件变换的四个颜色 recyclerview.setadapter(madapter); recyclerviewonitemclicklistener(); refresh(); loadmore(madapter); progress_loading_main.setvisibility(view.visible); initdata(); } @override public void ondestroyview() { super.ondestroyview(); butterknife.unbind(this); } /**进入页面的初始化数据*/ private void initdata(){ new handler().postdelayed(new runnable() { @override public void run() { netnewslist(true); progress_loading_main.setvisibility(view.gone); } }, 2000); } /**recyclerview每个item的点击事件*/ private void recyclerviewonitemclicklistener() { madapter.setonitemclicklistener(new myrecycleviewadapter.onitemclicklistener() { @override public void onitemclick(view view, int position) { snackbar.make(view, "fly", snackbar.length_short).show(); } }); } /** * 初始化recyclerview */ private void initrecyclerview() { // recyclerview.setitemanimator(new defaultitemanimator()); // recyclerview.sethasfixedsize(true); mrecycleviewlayoutmanager = new linearlayoutmanager(getactivity()); recyclerview.setlayoutmanager(mrecycleviewlayoutmanager); // 设置recycleview,显示是listview还是gridview还是瀑布流 } /**下拉刷新*/ private void refresh() { swiperefreshlayout.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() { @override public void onrefresh() { new handler().postdelayed(new runnable() { @override public void run() { netnewslist(true); swiperefreshlayout.setrefreshing(false); // 停止刷新 } }, 2000); } }); } /** * 设置上拉加载更多 * * @param adapter recyclerview适配器 */ public void loadmore(final myrecycleviewadapter adapter) { recyclerview.addonscrolllistener(new recyclerview.onscrolllistener() { private int lastvisibleitem; @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); lastvisibleitem = mrecycleviewlayoutmanager.findlastvisibleitemposition(); // 滑动到最后一个 } @override public void onscrollstatechanged(recyclerview recyclerview, int newstate) { super.onscrollstatechanged(recyclerview, newstate); // 效果在暂停时显示, 否则会导致重绘异常 if (newstate == recyclerview.scroll_state_idle && lastvisibleitem + 1 == adapter.getitemcount()) { if (lists != null && lists.size() >= 10) { // 真实开发中要设置mnews.size()大于加载分页显示的个数 adapter.loadlayout.setvisibility(view.visible); //加载更多 new handler().postdelayed(new runnable() { @override public void run() { netnewslist(false); } }, 2000); } } } }); } /** * 从网络加载数据列表 * * @param isrefresh 是否刷新 true 为刷新,false为不刷新 */ private void netnewslist(boolean isrefresh) { // viewdelegate.showloading(); if (isrefresh) { mpagenum = 1; } else { mpagenum++; } if (isrefresh) { if (!lists.isempty()) { lists.clear(); } } // todo 这里把页数mpagenum上传到服务端 lists.clear(); lists.addall(getdata()); madapter.notifydatasetchanged(); } private list<string> list = new arraylist<>(); private list<string> getdata() { for (int i = 0; i < 10; i++) { list.add(i + ""); } return list; } }
接下来是recycleview适配器中的xml文件:
这是正常item的布局,至于用cardview是为了让item展示出来的效果更好看
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.cardview xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:id="@+id/text" android:layout_width="match_parent" android:layout_height="50dp" android:text="" android:gravity="center"/> </android.support.v7.widget.cardview>
这是现实上拉加载的布局文件,作为不同item共同展示在recycleview上面:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/load_layout" android:visibility="gone" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" android:paddingbottom="12dip" android:paddingtop="12dip"> <com.rey.material.widget.progressview app:pv_autostart="true" app:pv_circular="true" app:pv_progressstyle="@style/material.drawable.circularprogress" app:pv_progressmode="indeterminate" android:layout_width="24dp" android:layout_height="24dp" /> <textview android:id="@+id/more_data_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="16sp" android:layout_marginleft="10dp" android:text="正在加载..." /> </linearlayout>
下面我们看看recycleview适配器的写法:
import android.app.activity; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.linearlayout; import android.widget.textview; import com.retrofit.wangfei.viewpagertablayout.r; import java.util.list; import butterknife.bind; import butterknife.butterknife; /** * created by android studio * description: recycleview的适配器 */ public class myrecycleviewadapter extends recyclerview.adapter { private final static int type_item = 0x01; private final static int type_footer = 0x02; private list<string> lists; private activity context; public linearlayout loadlayout; private onitemclicklistener monitemclicklistener; // 声明接口 public myrecycleviewadapter(list<string> lists, activity context) { this.lists = lists; this.context = context; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { if (type_item == viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.recycle_view_item, parent,false); itemviewholder itemviewholder = new itemviewholder(view); return itemviewholder; } else { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.list_footer, parent,false); loadlayout = (linearlayout) view.findviewbyid(r.id.load_layout); return new footviewholder(view); } } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { if (holder instanceof itemviewholder) { string text = lists.get(position); itemviewholder itemholder = (itemviewholder) holder; itemholder.text.settext(text); } } @override public int getitemcount() { return lists.size() + 1; } @override public int getitemviewtype(int position) { if (position + 1 == getitemcount()) { return type_footer; } else { return type_item; } } public class itemviewholder extends recyclerview.viewholder implements view.onclicklistener { @bind(r.id.text) textview text; public itemviewholder(view itemview) { super(itemview); butterknife.bind(this,itemview); itemview.setonclicklistener(this); } @override public void onclick(view v) { monitemclicklistener.onitemclick(v,getposition()); } } public class footviewholder extends recyclerview.viewholder { public footviewholder(view itemview) { super(itemview); } } /**调到外部使用*/ public void setonitemclicklistener(onitemclicklistener onitemclicklistener){ this.monitemclicklistener = onitemclicklistener; } /**定义接口*/ public interface onitemclicklistener{ void onitemclick(view v,int position); } }
最后是刷新控件的4个不同颜色:
public interface constance { /** * 下拉刷新控件变化的四个颜色 */ int[] colors = new int[] { android.r.color.holo_green_light, android.r.color.holo_blue_light, android.r.color.holo_green_light, android.r.color.holo_blue_light }; }
所需要的依赖库:
compile 'com.android.support:appcompat-v7:23.3.0' compile 'com.android.support:design:23.3.0' compile 'com.jakewharton:butterknife:7.0.1' compile 'com.android.support:support-v4:23.3.0' /**谷歌服务*/ compile 'com.google.android.gms:play-services:8.4.0' compile 'com.github.rey5137:material:1.2.2'
到这里就结束了,完成了下拉刷新和上拉加载更多的实现。
下面说说recyclerview的使用最基础的三点:
一:
recyclerview.sethasfixedsize(true); //方法用来使recyclerview保持固定的大小,该信息被用于自身的优化。
二:
recyclerview.setitemanimator(new defaultitemanimator());
itemanimator会根据适配器上收到的通知动画显示视图组的修改。基本上,它会自动显示添加和移除条目动画。这也不是一个简单的类,但我们发现defaultitemanimator已经可以运行得很好了。
三:
recyclerview.setlayoutmanager(mrecycleviewlayoutmanager); // 设置recycleview,显示是listview还是gridview还是瀑布流。 // 显示是listview linearlayoutmanager mrecycleviewlayoutmanager = new linearlayoutmanager(context); // 显示是gridview,参数一:上下文,参数二:列数 gridlayoutmanager mgridlayoutmanager=new gridlayoutmanager(context, 4); // 显示是瀑布流,参数一:显示几列,参数二:现实的方向,垂直或水平 staggeredgridlayoutmanager mstaggeredgridlayoutmanager=new staggeredgridlayoutmanager(2, staggeredgridlayoutmanager.vertical);
更多详细请参考:android recyclerview艺术般的控件使用完全解析
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android RecyclerView上拉加载和下拉刷新(基础版)
-
Android RecyclerView实现下拉刷新和上拉加载更多
-
android RecyclerView侧滑菜单,滑动删除,长按拖拽,下拉刷新上拉加载
-
Android RecyclerView自定义上拉和下拉刷新效果
-
Android控件PullRefreshViewGroup实现下拉刷新和上拉加载
-
android RecyclerView侧滑菜单,滑动删除,长按拖拽,下拉刷新上拉加载
-
Android控件PullRefreshViewGroup实现下拉刷新和上拉加载
-
微信小程序下拉加载和上拉刷新两种实现方法详解
-
Android RecyclerView下拉刷新和上拉加载及依赖
-
vue-scroller实现vue单页面的上拉加载和下拉刷新问题