BaseRecyclerViewAdapterHelper源码解读(七) 多布局

public class MultipleItem implements MultiItemEntity {

public static final int TEXT = 1;

public static final int IMG = 2;

public static final int IMG_TEXT = 3;

public static final int TEXT_SPAN_SIZE = 3;

public static final int IMG_SPAN_SIZE = 1;

public static final int IMG_TEXT_SPAN_SIZE = 4;

public static final int IMG_TEXT_SPAN_SIZE_MIN = 2;

private int itemType;

/**

  • 跨度

*/

private int spanSize;

public MultipleItem(int itemType, int spanSize, String content) {

this.itemType = itemType;

this.spanSize = spanSize;

this.content = content;

}

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() %

  1. {

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) {

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
图片转存中…(img-Kx4ZEoqm-1715502411870)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 27
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值