https://github.com/CymChad/BaseRecyclerViewAdapterHelper
这个框架是陈明宇开发的,封装了一个RecyclerView的adapter,不用替换原生的RecyclerView便能实现更多功能,比较方便。由于经常忘记如何使用,所以记录一下。
也可以访问这个地址,以前版本的说明,感觉比现在的说明详细些。
https://github.com/CymChad/BaseRecyclerViewAdapterHelper/wiki/old_doc-cn
先贴出adapter的代码,是不是比普通的RecyclerView的adapter简单许多。
public class adapter extends BaseQuickAdapter<Recharge.DataItem.ListItem,BaseViewHolder> {
public adapter( List<Recharge.DataItem.ListItem> data) {
super(R.layout.recyclerviewadapter, data);
}
@Override
protected void convert(BaseViewHolder helper, Recharge.DataItem.ListItem item) {
helper.setText(R.id.a,item.depositordersid)
.setText(R.id.b,item.cardNumber+"")
.setText(R.id.c,item.amount+"");
}
}
Recharge.DataItem.ListItem是json解析后的bean类,List<Recharge.DataItem.ListItem> data 这个是在外边传入的数据通过new adapter的时候设置的
Adapter adapter=new adapter(new ArrayList<Recharge.DataItem.ListItem>());
下面贴出Activity的代码
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.freevoip.easery.net.ApiUrls;
import com.freevoip.easery.net.MyCallBack;
import com.freevoip.easery.net.NetClient;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
private Gson gson;
private com.freevoip.easery.adapter adapter;
private SwipeRefreshLayout sw;
private RecyclerView ry;
private Recharge chongzhi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ry = (RecyclerView) findViewById(R.id.ry);
sw = (SwipeRefreshLayout) findViewById(R.id.sw);
adapter=new adapter(new ArrayList<Recharge.DataItem.ListItem>());
ry.setLayoutManager(new LinearLayoutManager(this));
adapter.setOnLoadMoreListener(this,ry);//必需指定ry 要不会闪退
ry.setAdapter(adapter);
// adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);//设置动画
// mCurrentCounter = adapter.getData().size();
gson = new Gson();
sw.setOnRefreshListener(this);
loadData(1,false);
}
//页数 每次下拉的时候
private int pageNum=1;
//创建一个集合用来存储网络请求回来的数据
private List<Recharge.DataItem.ListItem> dataSource=new ArrayList<>();
private void loadData(int pageNum, final boolean isloadmore) {
Map params = new LinkedHashMap();
params.put("staffid", "1" );
params.put("pageNo",pageNum );
params.put("pageSize","10");
NetClient.getInstance().doGet(ApiUrls.RECHARGE, params, new MyCallBack() {
@Override
public void onFailure(int code) {
}
@Override
public void onResponse(String json) {
chongzhi = gson.fromJson(json, Recharge.class);
if (chongzhi.status.equals("1")){
List<Recharge.DataItem.ListItem> list = chongzhi.data.list;
if (list!=null&&list.size()>0)
if (!isloadmore){
dataSource=list;
}else {
dataSource.addAll(list);
}
adapter.setNewData(dataSource);
adapter.notifyDataSetChanged();
}
}
});
}
private static final int PAGE_SIZE = 6;//为什么是6呢?
private boolean isErr;
private boolean mLoadMoreEndGone = false; //是否加载更多完毕
private int mCurrentCounter = 0;
int TOTAL_COUNTER=0;
@Override
public void onLoadMoreRequested() {
sw.setEnabled(false);//上拉加载的时候关闭下拉刷新 之后再打开
TOTAL_COUNTER = chongzhi.data.total;//总条数
int size = adapter.getData().size();
if (size < PAGE_SIZE) {
adapter.loadMoreEnd(true);
} else {
if (mCurrentCounter >= TOTAL_COUNTER) {
//TOTAL_COUNTER 为最大数量
// //数据全部加载完毕
// pullToRefreshAdapter.loadMoreEnd();//default visible
adapter.loadMoreEnd(mLoadMoreEndGone);//true is gone,false is visible
} else {
if (isErr) {
//成功获取更多数据 DataServer.getSampleData(PAGE_SIZE) 为设置新的数据集合
// adapter.addData(DataServer.getSampleData(PAGE_SIZE));
pageNum += 1;
loadData(pageNum, true);
mCurrentCounter = adapter.getData().size();
adapter.loadMoreComplete();
} else {
//获取更多数据失败
isErr = true;
adapter.loadMoreFail();
}
}
sw.setEnabled(true);
}
}
@Override
public void onRefresh() {
adapter.setEnableLoadMore(false);//下拉刷新的时候关闭上拉加载 之后再打开
sw.postDelayed(new Runnable() {
@Override
public void run() {
loadData(1,false);
isErr = false;
mCurrentCounter = PAGE_SIZE;//这行不能删除
pageNum=1;//页数置为1 才能继续重新加载
sw.setRefreshing(false);
adapter.setEnableLoadMore(true);
}
},2000);
}
}
经过以上步骤就可以使用 BaseRecyclerViewAdapterHelper了