现在的速度跟新的速度越来越快,手机的开发,变更也日夜加快普通的已经不能满足我们的需求了,我们看下面一张图
多布局
这张图就是一个简简单单的多布局,然而我们要是用普通的的视频去实现要写的代码量很多看着也很拥挤,逻辑也不清晰,那么我推荐大家使用一款适配器,代码量非常少,逻辑非常清晰 我们实现的使用的适配器是万能适配 万能适配器的官方网址:https://www.jianshu.com/p/b343fcff51b0
实体类必须实现MultiItemEntity,在设置数据的时候,需要给每一个数据设置itemType
public class MultipleItem implements MultiItemEntity {
public static final int TEXT = 1;
public static final int IMG = 2;
private int itemType;
public MultipleItem(int itemType) {
this.itemType = itemType;
}
@Override
public int getItemType() {
return itemType;
}
}
在构造里面addItemType绑定type和layout的关系
public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem, BaseViewHolder> {
public MultipleItemQuickAdapter(List data) {
super(data);
addItemType(MultipleItem.TEXT, R.layout.text_view);
addItemType(MultipleItem.IMG, R.layout.image_view);
}
@Override
protected void convert(BaseViewHolder helper, MultipleItem item) {
switch (helper.getItemViewType()) {
case MultipleItem.TEXT:
helper.setImageUrl(R.id.tv, item.getContent());
break;
case MultipleItem.IMG:
helper.setImageUrl(R.id.iv, item.getContent());
break;
}
}
}
长话短说我们看这一组具体实现的代码
public class FirstAdapter extends BaseMultiItemQuickAdapter<FirstBean.ResultBean.ListBean,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 */List<FirstBean.ResultBean.ListBean> data; public FirstAdapter(List<FirstBean.ResultBean.ListBean> data) { super(data); this.data=data; //跟据类型的不同加入不同的布局 addItemType(1, R.layout.item1); addItemType(2, R.layout.item2); addItemType(3, R.layout.item3); } @Override protected void convert(BaseViewHolder helper, FirstBean.ResultBean.ListBean item) { //根据返回的类型初始化控件 switch (helper.getItemViewType()){ case 1: { helper.setText(R.id.title1,item.getTitle()); helper.setText(R.id.title2,item.getTitle2()); Picasso.with(mContext).load(item.getFirstImg()).placeholder(R.mipmap.ic_launcher).into((ImageView)helper.getView(R.id.img1)); Picasso.with(mContext).load(item.getSecondImg()).placeholder(R.mipmap.ic_launcher).into((ImageView)helper.getView(R.id.img2)); } break; case 2: { helper.setText(R.id.title,item.getTitle()); Picasso.with(mContext).load(item.getFirstImg()).placeholder(R.mipmap.ic_launcher).into((ImageView)helper.getView(R.id.img)); } break; case 3: helper.setText(R.id.title,item.getTitle()); Picasso.with(mContext).load(item.getFirstImg()).placeholder(R.mipmap.ic_launcher).into((ImageView)helper.getView(R.id.img)); break; } } //根据要求返回不同的类型 @Override public int getItemViewType(int position) { if(data.get(position).getSecondImg()!=null){ return 1; }else if("2".equals(data.get(position).getMark())){ return 2; }else { return 3; } }}
|