BaseRecyclerViewAdapterHelper源码解读(七) 多布局

优点:

- 简单配置、无需重写额外方法。

- 可以设置默认布局类型

- 可以设置item布局的跨度

- 轻松添加新布局类型

使用方法:

1.写一个实体类,必须实现MultiItemEntity接口,并且尽量有itemType(用于标识该item是什么type)和spanSize(该item的跨度).

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

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

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

如何成为Android高级架构师!

架构师必须具备抽象思维和分析的能力,这是你进行系统分析和系统分解的基本素质。只有具备这样的能力,架构师才能看清系统的整体,掌控全局,这也是架构师大局观的形成基础。 你如何具备这种能力呢?一是来自于经验,二是来自于学习。

架构师不仅要具备在问题领域上的经验,也需要具备在软件工程领域内的经验。也就是说,架构师必须能够准确得理解需求,然后用软件工程的思想,把需求转化和分解成可用计算机语言实现的程度。经验的积累是需要一个时间过程的,这个过程谁也帮不了你,是需要你去经历的。

但是,如果你有意识地去培养,不断吸取前人的经验的话,还是可以缩短这个周期的。这也是我整理架构师进阶此系列的始动力之一。


成为Android架构师必备知识技能

对应导图的学习笔记(由阿里P8大牛手写,我负责整理成PDF笔记)

部分内容展示

《设计思想解读开源框架》

  • 目录
  • 热修复设计
  • 插件化框架设计

    《360°全方面性能优化》
  • 设计思想与代码质量优化
  • 程序性能优化
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

外链图片转存中…(img-sy3iYpRB-1711812057138)]
《360°全方面性能优化》
[外链图片转存中…(img-xJ74RXZN-1711812057138)]

  • 设计思想与代码质量优化
    [外链图片转存中…(img-oiIqJmrU-1711812057138)]
  • 程序性能优化
    [外链图片转存中…(img-ySMN03Kp-1711812057139)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值