初次使用RecyclerView的时候踩了很多坑,比如数据造好了adapter写好了set之后不是没数据就是显示不全要么就是崩溃各种难受,后来总算数据显示了,就要考虑刷新以及加载更多的问题了。起初我也是想的像当年实现ListView的刷新和加载更多一样添加headerView和footview,后来到网上看了一下原来已经有别的方式了,不用那么麻烦。
一、下拉刷新
1、至于下拉刷新只需要在RecyclerView外嵌套SwipeRefreshLayout即可实现下拉刷新效果
android:id="@+id/swip_live_pub" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/recy_live_pub" android:layout_width="match_parent" android:layout_height="match_parent"/>
2、SwipeRefreshLayout可以设置setOnRefreshListener在刷新键听里面就可以做自己要刷新的请求了
switchRefreshLayout.setOnRefreshListener(newSwipeRefreshLayout.OnRefreshListener() {
@Override
public voidonRefresh() {
//TODO做自己刷新相关的业务
//switchRefreshLayout.setRefreshing(false);//用于结束页面刷新进度效果
}
});
二、上拉加载更多
1、其实RecyclerView的RecyclerView.Adapter也可以实现同一列表页面多种布局效果,那么这样就好办了,我们只需要给列表结尾添加一个item显示加载更多的样式即可,照着这个思路往下走,页面时可以实现了。但是出现的时机怎么监听,怎么知道最后一条出现了去执行加载更多的异步请求。我们可以用recyclerView.addOnScrollListener(...)监听recyclerView的滑动事件,在滑动的过程中我们可以通过mLinearLayoutManager.findLastVisibleItemPosition();获取到当前显示的最后一项item的position,当这个position恰好是最后一项时执行加载更多,就这样变便实现了上啦加载的需求业务。
2、实现上拉加载代码
LinearLayoutManagermLinearLayoutManager;
inttotalItemCount;
intlastVisibleItem;
intfirstVisibleItem;
recyclerView.addOnScrollListener(newRecyclerView.OnScrollListener() {
@Override
public voidonScrollStateChanged(RecyclerView recyclerView,intnewState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public voidonScrolled(RecyclerView recyclerView,intdx,intdy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem=mLinearLayoutManager.findLastVisibleItemPosition();
firstVisibleItem=mLinearLayoutManager.findFirstVisibleItemPosition();
totalItemCount=mLinearLayoutManager.getItemCount();
if(lastVisibleItem==totalItemCount-1&&firstVisibleItem!=0){
//TODO在此处执行自己加载更多数据的异步
}
}
});
三、针对这个下拉刷新和上啦加载我自己封装了一个BaseRecyclerAdapter,只要继承它便可实现,我把代码以及使用方式贴上欢迎使用和指正
1、这个是BaseRecyclerAdapter
public abstract classBaseRecyclerAdapterextendsRecyclerView.AdapterimplementsView.OnClickListener, View.OnLongClickListener {
protectedContextmContext;
protectedListdatas;//数据
protectedLayoutInflatermInflate;
protected final intTYPE_ITEM=0;
protected final intTYPE_FOOTER=1;
private final intPAGE_SIZE=20;
protected finalStringTAG=this.getClass().getName();
privateOnRecyclerViewItemClickListenermOnItemClickListener=null;
private booleanisFootView=false;//根据加载数据的情况,判断是否需要继续显示加载
private booleanisLoading=false;
private booleanloadOver=false;
private booleanisNeedFootView=false;//是否需要拉加载更多,true需要,false不需要
//自定义监听事件
public static interfaceOnRecyclerViewItemClickListener {
voidonItemClick(View view);
voidonItemLongClick(View view);
}
public voidsetOnItemClickListener(OnRecyclerViewItemClickListener listener) {
mOnItemClickListener= listener;
}
//适配器初始化
publicBaseRecyclerAdapter(Context context, List datas) {
mContext=context;
this.datas=datas;
setHasStableIds(true);
}
@Override
public intgetItemViewType(intposition) {
//判断item类别
if(position !=datas.size()) {
returnTYPE_ITEM;
}else{
returnTYPE_FOOTER;
}
}
publicList getList(){
returndatas;
}
public voidsetList(List datas){
this.datas= datas;
}
@Override
publicRecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,intviewType) {
mInflate= LayoutInflater.from(mContext);
if(viewType !=TYPE_FOOTER){
returnonCreateItemViewHolder(parent, viewType);
}else{
View view =mInflate.inflate(R.layout.footer_load_view, parent,false);
return newFooterLoadHolder(view);
}
}
public abstractRecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent,intviewType);
@Override
public voidonBindViewHolder(RecyclerView.ViewHolder holder,intposition) {
}
@Override
public intgetItemCount() {
if(isNeedFootView){
if(isFootView)returndatas.size() +1;//获取数据的个数
else returndatas.size();
}else returndatas.size();
}
@Override
public longgetItemId(intposition) {
returnposition;
}
//点击事件回调
@Override
public voidonClick(View v) {
if(mOnItemClickListener!=null) {
mOnItemClickListener.onItemClick(v);
}
}
@Override
public booleanonLongClick(View v) {
if(mOnItemClickListener!=null) {
mOnItemClickListener.onItemLongClick(v);
}
return false;
}
//自定义ViewHolder,用于显示footer
classFooterLoadHolderextendsRecyclerView.ViewHolder {
publicFooterLoadHolder(View itemView) {
super(itemView);
}
}
LinearLayoutManagermLinearLayoutManager;
inttotalItemCount;
intlastVisibleItem;
intfirstVisibleItem;
public final voidregisterRefreshAndLoadMoreListener(RecyclerView recyclerView,finalSwipeRefreshLayout switchRefreshLayout,finalPullListener pullListener){
mLinearLayoutManager=(LinearLayoutManager) recyclerView.getLayoutManager();
switchRefreshLayout.setOnRefreshListener(newSwipeRefreshLayout.OnRefreshListener() {
@Override
public voidonRefresh() {
pullListener.pullToRefresh(newRefreshAndLoadNotify() {
@Override
public voidrefreshNotify(List refreshList) {
if(refreshList ==null|| refreshList.size() ==0){
ToastUtil.showToast("已是最新一条");
switchRefreshLayout.setRefreshing(false);
return;
}
datas.removeAll(refreshList);
datas.addAll(0,refreshList);
lastVisibleItem=mLinearLayoutManager.findLastVisibleItemPosition();
notifyDataSetChanged();
switchRefreshLayout.setRefreshing(false);
}
@Override
public voidloadMoreNotify(List list) {
}
});
}
});
recyclerView.addOnScrollListener(newRecyclerView.OnScrollListener() {
@Override
public voidonScrollStateChanged(RecyclerView recyclerView,intnewState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public voidonScrolled(RecyclerView recyclerView,intdx,intdy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem=mLinearLayoutManager.findLastVisibleItemPosition();
firstVisibleItem=mLinearLayoutManager.findFirstVisibleItemPosition();
totalItemCount=mLinearLayoutManager.getItemCount();
if(lastVisibleItem
isFootView=true;
notifyDataSetChanged();
return;
}
LogUtil.log(TAG, Log.ERROR, (isFootView&&lastVisibleItem==totalItemCount-1&&firstVisibleItem!=0)+"=========isLoading====="+isLoading);
if(isFootView&&lastVisibleItem==totalItemCount-1&&firstVisibleItem!=0){
if(isLoading)return;
isLoading=true;
pullListener.pullToLoadMore(newRefreshAndLoadNotify() {
@Override
public voidrefreshNotify(List list) {
}
@Override
public voidloadMoreNotify(List moreLoadList) {
if(moreLoadList ==null|| moreLoadList.size()
isFootView=false;
loadOver=true;
ToastUtil.showToast("已加载至最后一条");
}
if(moreLoadList !=null&& moreLoadList.size() >0){
datas.removeAll(moreLoadList);
datas.addAll(datas.size(), moreLoadList);
}
notifyDataSetChanged();
isLoading=false;
}
});
}
}
});
}
}
2、这个是footview
android:layout_width="match_parent" android:layout_height="60dip" android:background="@color/white" android:orientation="horizontal"> android:layout_width="match_parent" android:layout_height="30dip" android:layout_gravity="center" android:gravity="center" android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:background="@color/white" android:orientation="horizontal"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/white"/> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:text="加载中..."/>
3、数据通讯监听接口
/**
* Created by wangjingyun on 2017/6/7.
*
* 用于拉动后出发数据请求通信
*/
public interfacePullListener {
voidpullToRefresh(RefreshAndLoadNotify refreshAndLoad);
voidpullToLoadMore(RefreshAndLoadNotify refreshAndLoad);
}
4、使用:首先继承BaseRecyclerAdapter
其次在自己的activity里实现
private booleanrefreshing=false,loading=false;
yourAdapter.registerRefreshAndLoadMoreListener(recy_live_pub,swip_live_pub,newPullListener() {
@Override
public void pullToRefresh(RefreshAndLoadNotify refreshAndLoad) {
refreshing=true;
getLivePubList(refreshAndLoad);//异步请求数据,拿到数据后刷新页面
}
@Override
public void pullToLoadMore(RefreshAndLoadNotify refreshAndLoad) {
//TODO用法同刷新不在赘述
}
});
public void getLivePubList(RefreshAndLoadNotify refreshAndLoad){
//TODO异步请求拿数据
refreshAndLoad.refreshNotify(list);//异步请求拿到结果后在主线程执行
}