Android之RecyclerView轻松实现下拉刷新和加载更多示例
今天研究了下recyclerview的滑动事件,特别是下拉刷新和加载更多事件,在现在几乎所有的app显示数据列表时都用到了。自定义recyclerview下拉刷新和加载更多听上去很复杂,实际上并不难,只要是对滑动事件的监听和处理。
一、自定义recyclerview实现下拉刷新和加载更多
1、如何判断recyclerview是在上滑还是下滑
在recyclerview的onscrolllistener滑动事件监听中有个好用的方法,就是onscrolled(recyclerview recyclerview, int dx, int dy),其中根据dx的值的正负就可以判断是在左滑还是右滑,而根据dy的值就可以判断是在上滑还是下滑。
//上滑 if(dy>0){ //相应操作代码 } //下滑 else if(dy<0){ //相应操作代码 }
2、如何判断是否滑到了顶部或者底部
同样在recyclerview的onscrolllistener滑动事件监听中onscrolled(recyclerview recyclerview, int dx, int dy)方法中处理,根据canscrollvertically(int direction)来进行判断。
//是否滑到底部 if(!recyclerview.canscrollvertically(1)){ //相应处理操作 } //是否滑到顶部 if(!recyclerview.canscrollvertically(-1)){ //相应处理操作 }
3、自定义recyclerview
知道了滑动事件的判断和处理,就可以很轻松得实现下拉刷新和加载更多了。
import android.content.context; import android.support.annotation.nullable; import android.support.v7.widget.recyclerview; import android.util.attributeset; import android.util.log; /** * package:com.liuting.library * author:liuting * date:2017/2/14 * desc:自定义recycleview,下拉刷新以及上拉加载更多 */ public class refreshloadmorerecycleview extends recyclerview { private boolean isloadmore;//是否可以加载更多标志 private boolean isloadend;//加载到最后的标志 private boolean isloadstart;//顶部的标志 private boolean isrefresh;//是否可以下拉刷新标志 private int lastvisibleitem;//最后一项 private ionscrolllistener listener;//事件监听 public refreshloadmorerecycleview(context context) { super(context); init(context); } public refreshloadmorerecycleview(context context, @nullable attributeset attrs) { super(context, attrs); init(context); } public refreshloadmorerecycleview(context context, @nullable attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(context); } public void init(context context) { isloadend=false; isloadstart =true; this.addonscrolllistener(new recyclerview.onscrolllistener() { @override public void onscrollstatechanged(recyclerview recyclerview, int newstate) { super.onscrollstatechanged(recyclerview, newstate); //scroll_state_dragging 和 scroll_state_idle 两种效果自己看着来 if (newstate == recyclerview.scroll_state_idle) { if (isloadend) { // 判断是否已加载所有数据 if (isloadmore) {//未加载完所有数据,加载数据,并且还原isloadend值为false,重新定位列表底部 if (getlistener() != null) { getlistener().onloadmore(); } } else {//加载完了所有的数据 if(getlistener()!=null){ getlistener().onloaded(); } } isloadend = false; } else if (isloadstart) { if(isrefresh){ if (getlistener() != null) { getlistener().onrefresh(); } isloadstart=false; } } } } @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); //上滑 if(dy>0){ //是否滑到底部 if(!recyclerview.canscrollvertically(1)){ isloadend = true; }else{ isloadend = false; } } //下滑 else if(dy<0){ //是否滑到顶部 if(!recyclerview.canscrollvertically(-1)){ isloadstart=true; }else{ isloadstart=false; } } } }); } //监听事件 public interface ionscrolllistener { void onrefresh(); void onloadmore(); void onloaded(); } public ionscrolllistener getlistener() { return listener; } public void setlistener(ionscrolllistener listener) { this.listener = listener; } public boolean getloadmore() { return isloadmore; } //设置是否支持加载更多 public void setloadmoreenable(boolean loadmore) { isloadmore = loadmore; } public boolean getrefresh() { return isrefresh; } //设置是否支持下拉刷新 public void setrefreshenable(boolean refresh) { isrefresh = refresh; } }
二、实际用例
已经定义好了recyclerview,下面在demo中实际使用和处理。
1、定义布局
布局文件很简单,就是一个recyclerview
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.liuting.refreshloadmorelistview.mainactivity"> <com.liuting.library.refreshloadmorerecycleview android:id="@+id/main_recycle_view_data" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" /> </linearlayout>
2、定义recyclerview.adapter
recyclerview.adapter在这里就简单处理了,列表布局直接使用android自带的。
import android.content.context; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import java.util.list; /** * package:com.liuting.refreshloadmorelistview.adapter * author:liuting * date:2017/2/16 * desc:列表adapter */ public class refreshloadmorerecycleadapter extends recyclerview.adapter<refreshloadmorerecycleadapter.viewholder> { private list<string> list; private context context; public refreshloadmorerecycleadapter(context context,list<string> list) { this.context =context; this.list = list; } @override public refreshloadmorerecycleadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(android.r.layout.simple_expandable_list_item_1, parent, false); refreshloadmorerecycleadapter.viewholder viewholder = new refreshloadmorerecycleadapter.viewholder(view); return viewholder; } @override public void onbindviewholder(viewholder holder, int position) { holder.text.settext(list.get(position)); holder.itemview.settag(position); } @override public int getitemcount() { return list.size(); } class viewholder extends recyclerview.viewholder{ private textview text; public viewholder(view itemview) { super(itemview); text=(textview)itemview.findviewbyid(android.r.id.text1); } } }
3、在activity中定义好控件以及数据加载操作
实现自定义recyclerview中的数据加载事件监听,刷新、加载更多以及加载完成。
import android.app.progressdialog; import android.os.bundle; import android.os.handler; import android.os.message; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.linearlayoutmanager; import android.widget.toast; import com.liuting.library.refreshloadmorerecycleview; import com.liuting.refreshloadmorelistview.adapter.refreshloadmorerecycleadapter; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity implements refreshloadmorerecycleview.ionscrolllistener{ private refreshloadmorerecycleview recycleview;//下拉刷新recycleview private list<string> list;//列表 private refreshloadmorerecycleadapter adapter;//adapter private progressdialog dialog;//提示框 private static final int refresh_load=0;//下拉刷新 private static final int more_load=1;//加载更多 private handler handler =new handler(){ @override public void handlemessage(message msg) { super.handlemessage(msg); switch (msg.what){ case refresh_load: recycleview.setloadmoreenable(true); dismissdialog(); if(list!=null){ list.clear(); } loaddata(); adapter.notifydatasetchanged(); break; case more_load: recycleview.setloadmoreenable(false); dismissdialog(); loaddata(); adapter.notifydatasetchanged(); break; } } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); } public void initview(){ dialog = new progressdialog(mainactivity.this); list=new arraylist<>(); loaddata(); recycleview = (refreshloadmorerecycleview)findviewbyid(r.id.main_recycle_view_data); final linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(mainactivity.this); recycleview.setlayoutmanager(linearlayoutmanager); adapter = new refreshloadmorerecycleadapter(mainactivity.this,list); recycleview.setadapter(adapter); recycleview.setlistener(this); recycleview.setrefreshenable(true); recycleview.setloadmoreenable(true); } /** * 加载数据 */ public void loaddata(){ for(int i=0;i<10;i++ ){ list.add("it is "+i); } } @override public void onrefresh() { showdialog(); new thread(){ @override public void run() { super.run(); try { sleep(5000); handler.sendemptymessage(refresh_load); } catch (interruptedexception e) { e.printstacktrace(); } } }.start(); } @override public void onloadmore() { showdialog(); new thread(){ @override public void run() { super.run(); try { sleep(5000); handler.sendemptymessage(more_load); } catch (interruptedexception e) { e.printstacktrace(); } } }.start(); } @override public void onloaded() { toast.maketext(mainactivity.this,"loaded all",toast.length_short).show(); } /** * dismiss dialog */ private void dismissdialog(){ if (dialog!=null&&dialog.isshowing()){ dialog.dismiss(); } } /** * show dialog */ private void showdialog(){ if (dialog!=null&&!dialog.isshowing()){ dialog.show(); } } }
三、最终效果图
到这里就轻松实现了recyclerview的下拉刷新和加载更多了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。