【Android】BRVAH多布局实现

前言

基于3.0.4版本的BRVAH框架实现的

实现方法

1.创建多个不同类型的布局(步骤忽略)
2.创建数据实体类

数据类要实现【MultiItemEntity】接口

kotlin 复制代码
class MyMultiItemEntity(
    //获取布局类型
    override var itemType: Int,
    var tractorRes: Int? = null,
    var tractorTitle: String? = null,
    // 4-set 第一种布局需要的数据
    var fourSetFirstName: String? = null,
    var fourSetSecondName: String? = null,
    var fourSetThreeName: String? = null,
    var fourSetFourName: String? = null,
    // 5-set 第二种布局需要的数据
    var fiveSetFirstName: String? = null,
    var fiveSetSecondName: String? = null,
    var fiveSetThreeName: String? = null,
    var fiveSetFourName: String? = null,
    var fiveSetFiveName: String? = null,
    // 6-set 第三种布局需要的数据
    var sixSetFirstName: String? = null,
    var sixSetSecondName: String? = null,
    var sixSetThreeName: String? = null,
    var sixSetFourName: String? = null,
    var sixSetFiveName: String? = null,
    var sixSetSixName: String? = null
) : MultiItemEntity {

   //设置三个字段(因为有三种布局)
    companion object {
        const val TRACTOR_ITEM_FOUR = 0
        const val TRACTOR_ITEM_FIVE = 1
        const val TRACTOR_ITEM_SIX = 2
    }

//使用此构造方法构建第一种布局的数据源
    constructor(
        itemType: Int,
        res: Int,
        title: String,
        first: String,
        second: String,
        three: String,
        four: String
    ) : this(itemType) {
        this.tractorRes = res
        this.tractorTitle = title
        this.fourSetFirstName = first
        this.fourSetSecondName = second
        this.fourSetThreeName = three
        this.fourSetFourName = four
    }
//使用此构造方法构建第二种布局的数据源
    constructor(
        itemType: Int,
        res: Int,
        title: String,
        first: String,
        second: String,
        three: String,
        four: String,
        five: String
    ) : this(itemType) {
        this.tractorRes = res
        this.tractorTitle = title
        this.fiveSetFirstName = first
        this.fiveSetSecondName = second
        this.fiveSetThreeName = three
        this.fiveSetFourName = four
        this.fiveSetFiveName = five
    }
//使用此构造方法构建第三种布局的数据源
    constructor(
        itemType: Int,
        res: Int,
        title: String,
        first: String,
        second: String,
        three: String,
        four: String,
        five: String,
        six: String
    ) : this(itemType) {
        this.tractorRes = res
        this.tractorTitle = title
        this.sixSetFirstName = first
        this.sixSetSecondName = second
        this.sixSetThreeName = three
        this.sixSetFourName = four
        this.sixSetFiveName = five
        this.sixSetSixName = six
    }
}
3.创建Adapter

需要继承自【BaseMultiItemQuickAdapter】

kotlin 复制代码
class SteerAdapter(
    val list: MutableList<MyMultiItemEntity>,
) :
    BaseMultiItemQuickAdapter<MyMultiItemEntity, BaseViewHolder>(list) {


    init {
        addItemType(
            MyMultiItemEntity.TRACTOR_ITEM_FOUR,
            R.layout.item_tractor_second_four_sets
        )
        addItemType(
            MyMultiItemEntity.TRACTOR_ITEM_FIVE,
            R.layout.item_tractor_second_five_sets
        )
        addItemType(
            MyMultiItemEntity.TRACTOR_ITEM_SIX,
            R.layout.item_tractor_second_six_sets
        )
    }

    override fun convert(holder: BaseViewHolder, item: MyMultiItemEntity) {
        when (holder.itemViewType) {
            MyMultiItemEntity.TRACTOR_ITEM_FOUR -> {

}
            MyMultiItemEntity.TRACTOR_ITEM_FIVE -> {
              
}                    

            MyMultiItemEntity.TRACTOR_ITEM_SIX -> {
                    
    }
}

使用

kotlin 复制代码
//        binding.rvList.layoutManager = LinearLayoutManager(context)
//        val myAdapter = SteerAdapter(list)
//        val dp10 = requireContext().resources.getDimensionPixelSize(R.dimen.dp_10)
//        val itemDecoration = object : RecyclerView.ItemDecoration() {
//            override fun getItemOffsets(
//                outRect: Rect,
//                view: View,
//                parent: RecyclerView,
//                state: RecyclerView.State
//            ) {
//                outRect.bottom = dp10
//            }
//        }
//        binding.rvList.addItemDecoration(itemDecoration)
//        binding.rvList.adapter = myAdapter

基本上主要代码就这么多,基本上对着官方代码来就可以了,但是有一点就是官方代码对于数据类的构建讲的比较粗略,这篇笔记也是对这个进行补充吧。

相关推荐
Crossoads1 小时前
【汇编语言】外中断(一)—— 外中断的魔法:PC机键盘如何触发计算机响应
android·开发语言·数据库·深度学习·机器学习·计算机外设·汇编语言
sunphp开发者2 小时前
黑客攻击网站,篡改首页问题排查修复
android·js
我又来搬代码了2 小时前
【Android Studio】创建新项目遇到的一些问题
android·ide·android studio
ggs_and_ddu6 小时前
Android--java实现手机亮度控制
android·java·智能手机
zhangphil12 小时前
Android绘图Path基于LinearGradient线性动画渐变,Kotlin(2)
android·kotlin
watl012 小时前
【Android】unzip aar删除冲突classes再zip
android·linux·运维
键盘上的蚂蚁-12 小时前
PHP爬虫类的并发与多线程处理技巧
android
喜欢猪猪13 小时前
Java技术专家视角解读:SQL优化与批处理在大数据处理中的应用及原理
android·python·adb
JasonYin~15 小时前
HarmonyOS NEXT 实战之元服务:静态案例效果---手机查看电量
android·华为·harmonyos
zhangphil15 小时前
Android adb查看某个进程的总线程数
android·adb