RecyclerView框架封装之BaseRecyclerVie
2018-10-09 本文已影响53人
我的资讯圈
框架官网地址:
官方网站:www.recyclerview.org
第1步: 初始化swipe和recyclerView
initRecyclerView(this, getActivity(), mRefreshLayout, mRecyclerView, LinearLayoutManager.VERTICAL);
第2步:初始化适配器
/**
* 初始化适配器
*/
private void initAdapter() {
if (null == mAdapter) {
mAdapter = new MyPurchaseRecordsAdapter(R.layout.my_purchase_records_item, mBean.getBills());
mRecyclerView.setAdapter(mAdapter);
//添加加载更多的监听
mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
@Override
public void onLoadMoreRequested() {
if (mBean.getLastid() == -1) {
//数据全部加载完毕
mAdapter.loadMoreEnd();
} else {
loadMore();
}
}
}, mRecyclerView);
/*item点击事件*/
mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
if (adapter.getData().size() > position) {
Intent intent = new Intent(MyPurchaseRecordsActivity.this, TRDetailActivity.class);
int id = ((SMsTransactionRecordBean.PayRecordItem) adapter.getData().get(position)).getId();
LogUtil.i((adapter.getData().get(position)).toString());
intent.putExtra("id", id);
startActivity(intent);
}
}
});
}
}
第3步:数据加载成功后是否继续加载更多
if (mBean.getBills() != null) {
initAdapter();
if (isLoadMore) {//加载更多
//给列表添加数据
mAdapter.addData(mBean.getBills());
if (mBean.getLastid() == -1) {//全部加载完
mAdapter.loadMoreEnd();
} else {//加载完一部分
mAdapter.loadMoreComplete();
}
isLoadMore = false;
} else if (isRefresh) {//刷新数据
mAdapter.replaceData(mBean.getBills());
mRefreshLayout.setRefreshing(false);
isRefresh = false;
}
}
}
第4步:适配器分两种,单类型布局和多类型布局
(1)单类型布局
public class MyPurchaseRecordsAdapter extends BaseQuickAdapter<SMsPurchaseRecordsBean.DistrictShopItem, BaseViewHolder>{
public MyPurchaseRecordsAdapter(@LayoutRes int layoutResId, @Nullable List<SMsPurchaseRecordsBean.DistrictShopItem> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, SMsPurchaseRecordsBean.DistrictShopItem item) {
}
}
(2)多类型布局
/**
* Describe:xxxxx
* <p>
* Author: JeremyHan
* <p>
* Create time: 2018/9/4 上午11:45
*/
public class MyBuySellTerritoryAdapter extends BaseQuickAdapter<SMsBuySellTerritoryBean.OwnSellLordItem, BaseViewHolder> {
private static final int ENTITY_TYPE1 = 1;//自己的
private static final int ENTITY_TYPE2 = 2;//卖出的
private List<PersonalProvinceModel> mProvinceList;
public MyBuySellTerritoryAdapter() {
super(null);
//Step.1
setMultiTypeDelegate(new MultiTypeDelegate<SMsBuySellTerritoryBean.OwnSellLordItem>() {
@Override
protected int getItemType(SMsBuySellTerritoryBean.OwnSellLordItem entity) {
return entity.type;
}
});
//Step.2
getMultiTypeDelegate()
.registerItemType(ENTITY_TYPE1, R.layout.my_territory_own_item)
.registerItemType(ENTITY_TYPE2, R.layout.my_territory_sale_item);
}
@Override
protected void convert(BaseViewHolder helper, SMsBuySellTerritoryBean.OwnSellLordItem entity) {
Gson gson = new Gson();
//Step.3
switch (helper.getItemViewType()){
case ENTITY_TYPE1:
break;
case ENTITY_TYPE2:
break;
}
}
}
第5步:加载更多
/**
* 加载更多
*/
private void loadMore() {
//消息请求
}
扩展:如果你想添加头部
/**
* 添加头部
*/
private void initHeader() {
View view = View.inflate(this, R.layout.my_territory_header, null);
ButterKnife.bind(this, view);
mBuySellAdapter.addHeaderView(view);
}
就这么简单,一套列表就搞定了。