}
public MultipleItem(int itemType, int spanSize) {
this.itemType = itemType;
this.spanSize = spanSize;
}
public int getSpanSize() {
return spanSize;
}
public void setSpanSize(int spanSize) {
this.spanSize = spanSize;
}
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public int getItemType() {
return itemType;
}
}
2.定义一个adapter,继承自BaseMultiItemQuickAdapter(必须),然后在构造方法中添加你这个RecyclerView的相关的item全部添加上。
然后在convert()方法里面绑定数据即可,绑定数据的时候记得如果添加了header,那么需要索引需要-1。
public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem, BaseViewHolder> {
public MultipleItemQuickAdapter(Context context, List data) {
super(data);
addItemType(MultipleItem.TEXT, R.layout.item_text_view);
addItemType(MultipleItem.IMG, R.layout.item_image_view);
addItemType(MultipleItem.IMG_TEXT, R.layout.item_img_text_view);
}
@Override
protected void convert(BaseViewHolder helper, MultipleItem item) {
switch (helper.getItemViewType()) {
case MultipleItem.TEXT:
helper.setText(R.id.tv, item.getContent());
break;
case MultipleItem.IMG_TEXT:
switch (helper.getLayoutPosition() %
- {
case 0:
helper.setImageResource(R.id.iv, R.mipmap.animation_img1);
break;
case 1:
helper.setImageResource(R.id.iv, R.mipmap.animation_img2);
break;
}
break;
}
}
}
定做一个BaseMultiItemQuickAdapter
在定制adapter之前,我们先来了解一下SparseIntArray
SparseIntArrays map integers to integers. Unlike a normal array of integers, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Integers, both because it avoids auto-boxing keys and values and its data structure doesn’t rely on an extra entry object for each mapping.
Note that this container keeps its mappings in an array data structure, using a binary search to find keys. The implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.
It is possible to iterate over the items in this container using keyAt(int) and valueAt(int). Iterating over the keys using keyAt(int) with ascending values of the index will return the keys in ascending order, or the values corresponding to the keys in ascending order in the case of valueAt(int).
上面是官方介绍,下面是翻译:
SparseIntArrays将整数映射到整数。与正常的整数数组不同,索引中可能存在差距。它旨在比使用HashMap将整数映射到整数更有效率,因为它避免了自动打包(装箱)键和值,并且其数据结构不依赖于每个映射的额外的条目对象。
请注意,此容器将其映射保存在数组数据结构中,使用二进制搜索查找密钥。该实现并不适用于可能包含大量项目的数据结构。它通常比传统的HashMap更慢,因为查找需要二进制搜索,并添加和删除需要插入和删除数组中的条目。对于容纳数百种物品的容器,性能差异不显着,小于50%。
可以使用keyAt(int)和valueAt(int)迭代此容器中的项目。使用keyAt(int)使用keyAt(int)迭代索引的值将以升序返回键值,或者在valueAt(int)的情况下按升序对应的键值。
由于我们是需要实现多布局,于是我们需要引入不同的type和对应的不同的布局,这里开源库中使用的是SparseIntArray来存储这些数据.key是type,value是layoutResId.
下面我们来看看adapter源码
public abstract class BaseMultiItemQuickAdapter<T extends MultiItemEntity, K extends BaseViewHolder> extends BaseQuickAdapter<T, K> {
/**
-
layouts indexed with their types
-
key是type,value是layoutResId
*/
private SparseIntArray layouts;
private static final int DEFAULT_VIEW_TYPE = -0xff;
public static final int TYPE_NOT_FOUND = -404;
/**
-
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 BaseMultiItemQuickAdapter(List data) {
super(data);
}
@Override
protected int getDefItemViewType(int position) {
Object item = mData.get(position);
// 实体类必须实现MultiItemEntity接口 不然不知道item的类型
if (item instanceof MultiItemEntity) {
return ((MultiItemEntity) item).getItemType();
}
return DEFAULT_VIEW_TYPE;
}
/**
- 设置默认的type的布局
*/
protected void setDefaultViewTypeLayout(@LayoutRes int layoutResId) {
addItemType(DEFAULT_VIEW_TYPE, layoutResId);
}
@Override
protected K onCreateDefViewHolder(ViewGroup parent, int viewType) {
return createBaseViewHolder(parent, getLayoutId(viewType));
}
private int getLayoutId(int viewType) {
return layouts.get(viewType, TYPE_NOT_FOUND);
}
/**
-
增加item类型
-
@param type item类型
-
@param layoutResId item布局文件
*/
protected void addItemType(int type, @LayoutRes int layoutResId) {
if (layouts == null) {
layouts = new SparseIntArray();
}
layouts.put(type, layoutResId);