BaseRecycleViewAdapterHelper在githhub上是一个很火的自行封装的RecycleView适配器
集成了大部分列表的需求解决方案。这里主要是讲下BaseRecycleViewAdapterHelper多布局的使用。
一、
1、在build.gradle的repositories中添加maven{url"https://jitpack.io"}
2、添加依赖,一个是RecycleView,一个是BaseRecyeleViewAdapterHelper
compile"com.Android.support:recyclerview-v7:25.3.1"compile"com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.18"
二、实体类实现MultipleItemEnty
public class MultipleItem implements MultiItemEntity{
public static final int TEXT=1;
public static final int IMAGE=2;
public static final int IMAGE_TEXT=3;
private int itemType;
private String content;
public String getContent() {
return content;
}
public MultipleItem(int itemType,String content){
this.itemType=itemType;
this.content=content;
}
@Override
public int getItemType() {
return itemType;
}
}
三、在设置数据的时候,需要设置ItemType
private void iniData() {
for(int i=0;i<15;i++){
list.add(new MultipleItem(MultipleItem.TEXT,"text"+i));
list.add(new MultipleItem(MultipleItem.IMAGE_TEXT,"image"+i));
}
}
四、继承BaseMultipleItemQuickAdapter,并在适配器构造方法中,addItemType绑定type和layout的关系
public class MultipleItemAdapter extends BaseMultiItemQuickAdapter{
public MultipleItemAdapter(Context context, List data) {
super(data);
//自定义的多布局
addItemType(MultipleItem.TEXT,R.layout.item_text);
addItemType(MultipleItem.IMAGE_TEXT,R.layout.item_image_text);
}
@Override
protected void convert(BaseViewHolder helper, MultipleItem item) {
switch(helper.getItemViewType()){
case MultipleItem.TEXT:
helper.setText(R.id.tv,item.getContent());
break;
case MultipleItem.IMAGE_TEXT:
helper.setImageResource(R.id.imageView,R.mipmap.ic_launcher);
helper.setText(R.id.tv,item.getContent());
break;
}
}
}
源码地址:https://github.com/shenshizhong/RecycleViewMultipleItem
我的博客:http://blog.csdn.NET/shenshizhong
我的:http://www.jianshu.com/users/345daf0211ad/latest_articles