Skip to content

Use it custom item view type

Allen edited this page Aug 22, 2016 · 2 revisions

Show

Use it custom item view type

Getting started

  1. Entity must inherit MultiItemEntity
public class MultipleItem implements MultiItemEntity {
    public static final int TEXT = 1;
    public static final int IMG = 2;
    private int itemType;


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

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

    @Override
    public int getItemType() {
        return itemType;
    }
}
  1. binding itemType with layout
public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem> {

    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;
        }
    }

}

If there are multiple layout item two rows layout is the same, as shown: multiple_demo

Through the above picture, we can see that the second line is the third line item also ImageView, this time ImageView can be reused, we can use GridLayoutManager

 multipleItemAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                return data.get(position).getSpanSize();
            }
        });

By rewriting his SapnSize reuse ImageView. Suppose GridLayoutManager of spanCount is 3, then the first, second, item 3 of spanSize is because to occupy a line (and weight similar meaning), then the third, fourth, fifth, item of spanSize It is 1.

Welcome | New issue

Clone this wiki locally