(只有下拉刷新和上拉加载的简单小Demo:下载链接:http://download.csdn.net/download/qq_24800377/10020934)
LRecyclerView是支持addHeaderView、 addFooterView、下拉刷新、分页加载数据的RecyclerView。
它对 RecyclerView 控件进行了拓展,给RecyclerView增加HeaderView、FooterView,并且不需要对你的Adapter做任何修改。
在此感谢第三方提供者……
使用(最下方给出封装好的Adapter和例子的TestAdapter)
第一步:
在build.gradle中,添加JitPack仓库依赖
//增加JitPack仓库依赖 repositories{ maven { url "https://jitpack.io" } }
第二步:
还是build.gradler中,添加LRecyclerView依赖
//添加LRecyclerView 依赖 compile 'com.github.jdsjlzx:LRecyclerView:1.4.3'
第三步:
添加布局文件
xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"> <com.github.jdsjlzx.recyclerview.LRecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"/> RelativeLayout>
第四步(使用):
在mainAvtivity中初始化View.
//初始化view private void initView(){ recyclerView = (LRecyclerView) findViewById(R.id.list); //下拉刷新 recyclerView.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { Log.e(TAG,"下拉刷新……"); getData(); showView(); } }); //上拉加载 更多 recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore() { Log.e(TAG,"上拉加载更多……"); getData(); showView(); } }); }
//展示View
private void showView(){ TestAdapter adapter = new TestAdapter(this,getData()); lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter); recyclerView.setAdapter(lRecyclerViewAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); } //获取数据方法 private ListgetData(){ List strList = new ArrayList(); for(int i=0;i<20;i++){ strList.add("包子"); } recyclerView.refreshComplete(10);//加载完成,结束动画布局,参数为一页最大数 return strList; }
项目中使用的TestAdapter:
/** * 测试adapter */ public class TestAdapter extends ListBaseAdapter{ public TestAdapter(Context context,List strList){ super(context); setDataList(strList);//填充数据 } @Override public int getLayoutId() { return R.layout.adapter_text_item; } @Override public void onBindItemHolder(SuperViewHolder holder, int position) { String data = getDataList().get(position); TextView tv = holder.getView(R.id.tv); tv.setText("当前position:"+position+",数据:"+data); } }
封装好的ListBaseAdapter
/** * 封装adapter(注意:仅供参考,根据需要选择使用demo中提供的封装adapter) * @param数据类型 */ public abstract class ListBaseAdapter<T> extends RecyclerView.Adapter { protected Context mContext; private LayoutInflater mInflater; protected List<T> mDataList = new ArrayList<>(); public ListBaseAdapter(Context context) { mContext = context; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public SuperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = mInflater.inflate(getLayoutId(), parent, false); return new SuperViewHolder(itemView); } @Override public void onBindViewHolder(SuperViewHolder holder, int position) { onBindItemHolder(holder, position); } //局部刷新关键:带payload的这个onBindViewHolder方法必须实现 @Override public void onBindViewHolder(SuperViewHolder holder, int position, List