【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

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

相关推荐
Lei活在当下2 小时前
【Perfetto从入门到精通】2. 使用 Perfetto 追踪/分析 APP 的 Native/Java 内存
android·性能优化·架构
愤怒的代码2 小时前
🔗 深度解析 SystemUI 进程间通信机制(一)
android·操作系统·app
RainyJiang3 小时前
聊聊协程里的 Semaphore:别让协程挤爆门口
android·kotlin
Dev7z5 小时前
在MySQL里创建数据库
android·数据库·mysql
invicinble5 小时前
mysql建立存数据的表(一)
android·数据库·mysql
似霰6 小时前
传统 Hal 开发笔记1----传统 HAL简介
android·hal
Zender Han7 小时前
Flutter Gradients 全面指南:原理、类型与实战使用
android·flutter·ios
火柴就是我7 小时前
Flutter Path.computeMetrics() 的使用注意点
android·flutter
モンキー・D・小菜鸡儿8 小时前
Android 系统TTS(文字转语音)解析
android·tts
2501_915909068 小时前
iOS 反编译防护工具全景解析 从底层符号到资源层的多维安全体系
android·安全·ios·小程序·uni-app·iphone·webview