一、BaseMultiItemQuickAdapter使用步骤
1、Bean继承implements MultiItemEntity 并实现其方法
public class SmartRefreshBean implements MultiItemEntity {
/**
* id : 24
* name : 时尚
* alias : null
* description : 优雅地行走在潮流尖端
* bgPicture : http://img.kaiyanapp.com/22192a40de238fe853b992ed57f1f098.jpeg
* bgColor :
* headerImage : http://img.kaiyanapp.com/c9b19c2f0a2a40f4c45564dd8ea766d3.png
* defaultAuthorId : 2160
*/
private int id;
private String name;
private Object alias;
private String description;
private String bgPicture;
private String bgColor;
private String headerImage;
private int defaultAuthorId;
private int itemType=0;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getAlias() {
return alias;
}
public void setAlias(Object alias) {
this.alias = alias;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getBgPicture() {
return bgPicture;
}
public void setBgPicture(String bgPicture) {
this.bgPicture = bgPicture;
}
public String getBgColor() {
return bgColor;
}
public void setBgColor(String bgColor) {
this.bgColor = bgColor;
}
public String getHeaderImage() {
return headerImage;
}
public void setHeaderImage(String headerImage) {
this.headerImage = headerImage;
}
public int getDefaultAuthorId() {
return defaultAuthorId;
}
public void setDefaultAuthorId(int defaultAuthorId) {
this.defaultAuthorId = defaultAuthorId;
}
public void setItemType(int itemType) {
this.itemType = itemType;
}
@Override
public int getItemType() {
return itemType;
}
}
2、Adapter继承BaseMultiItemQuickAdapter
public class SmartRefreshMultiAdapter extends BaseMultiItemQuickAdapter<SmartRefreshBean, BaseViewHolder> {
/**
* Same as QuickAdapter#QuickAdapter(Context,int) but with
* some initialization data.
*
* @param data A new list is created out of this one to avoid mutable list
*/
public SmartRefreshMultiAdapter(List<SmartRefreshBean> data) {
super(data);
addItemType(1, R.layout.item_smart_refresh);
addItemType(2, R.layout.item_smart_refresh_2);
}
@Override
protected void convert(BaseViewHolder helper, SmartRefreshBean item) {
switch (helper.getItemViewType()) {
case 1:
helper.setText(R.id.tv_item_smart_refresh, item.getName());
GlideApp.with(mContext).load(item.getBgPicture()).into((ImageView) helper.getView(R.id.iv_item_smart_refresh));
break;
case 2:
helper.setText(R.id.tv_item_smart_refresh_2, item.getName());
break;
}
}
}
3、presenter提供数据并设置ItemType
public void getData() {
model.getSmartRefreshData()
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(RxLifeCycleUtils.<List<SmartRefreshBean>>bindToLifecycle(view))
.map(new Function<List<SmartRefreshBean>, List<SmartRefreshBean>>() {
@Override
public List<SmartRefreshBean> apply(List<SmartRefreshBean> smartRefreshBeanList) throws Exception {
List<SmartRefreshBean> list = new ArrayList<>();
for (int i = 0; i < smartRefreshBeanList.size(); i++) {
if (i % 2 == 0) {
smartRefreshBeanList.get(i).setItemType(1);
} else {
smartRefreshBeanList.get(i).setItemType(2);
}
list.add(smartRefreshBeanList.get(i));
}
return list;
}
})
.subscribe(new CustomerSubscribe<List<SmartRefreshBean>>() {
@Override
public void onNext(List<SmartRefreshBean> smartRefreshBeans) {
view.showContent(smartRefreshBeans);
}
});
}
4、具体调用
public class SmartRefreshActivity extends BaseActivity<SmartRefreshPresenter> implements SmartRefreshContract.ISmartRefreshView {
@BindView(R.id.srl_smart_refresh)
SmartRefreshLayout srl_smart_refresh;
@BindView(R.id.rv_smart_refresh)
RecyclerView rv_smart_refresh;
// private SmartRefreshAdapter adapter;
private SmartRefreshMultiAdapter adapter;
private List<SmartRefreshBean> smartRefreshBeanList = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smart_refresh);
ButterKnife.bind(this);
presenter = new SmartRefreshPresenter(this);
srl_smart_refresh.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshLayout) {
presenter.getData();
}
});
srl_smart_refresh.setEnableLoadMore(false);
// adapter = new SmartRefreshAdapter(smartRefreshBeanList);
adapter = new SmartRefreshMultiAdapter(smartRefreshBeanList);
rv_smart_refresh.setLayoutManager(new LinearLayoutManager(this));
rv_smart_refresh.setItemAnimator(new DefaultItemAnimator());
rv_smart_refresh.setHasFixedSize(true);
rv_smart_refresh.setAdapter(adapter);
srl_smart_refresh.autoRefresh();
}
@Override
public void showContent(List<SmartRefreshBean> smartRefreshBeans) {
srl_smart_refresh.finishRefresh();
srl_smart_refresh.finishLoadMore();
smartRefreshBeanList.clear();
smartRefreshBeanList.addAll(smartRefreshBeans);
adapter.notifyDataSetChanged();
}
}