• 【Android】BRVAH多布局实现


    前言

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

    实现方法

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

    数据类要实现【MultiItemEntity】接口

    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
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    3.创建Adapter

    需要继承自【BaseMultiItemQuickAdapter】

    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 -> {
                        
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    使用

    //        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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

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

  • 相关阅读:
    vscode按ctrl+鼠标左键没反应
    上海亚商投顾:沪指再创年内新低 贵州茅台等权重股集体调整
    AlphaControls控件TsRadioGroup的使用
    UE4 C++:TSet容器
    常用redis-lua脚本
    Spring Cloud Gateway微服务网关快速入门
    基础gdb操作【Linux】
    卷妹带你回顾Java基础(一)每日更新Day11
    改进的Salp Swarm优化算法(ISSA)(Matlab代码实现)
    创新性3D数据合成模型,微软推出EgoGen
  • 原文地址:https://blog.csdn.net/qq_43358469/article/details/133934554