Android开发RecyclerView之BRVAH多条目

1,第一步 在你的项目的Build.gradle中添加

 allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

2,在你的Module的build.gradle中添加

dependencies {
            compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
    }

3,在你的实体类中必须实现MultiItemEntity,在设置数据的时候,需要给每一个数据设置itemType

public class MultipleItem implements MultiItemEntity {
    public static final int TEXT = 0;
    public static final int IMG = 1;
    private int itemType=0;

    public MultipleItem(int itemType) {
        this.itemType = itemType;
    }

    @Override
    public int getItemType() {
        int i = getCommodityName().length()%2;
	itemType = i;
	return itemType;
    }
}

4,你的适配器

public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter {

    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 0:
                helper.setImageUrl(R.id.tv, item.getContent());
                break;
            case 1:
                helper.setImageUrl(R.id.iv, item.getContent());
                break;
        }
    }

}

5,设置适配器

LinearLayoutManager linnearLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recy.setLayoutManager(linnearLayoutManager);
MultipleItemQuickAdapter adapter=new MultipleItemQuickAdapter(list);
recy.setAdapter(adapter);

你可能感兴趣的:(个人)