【Android】BRVAH多布局实现

编程入门 行业动态 更新时间:2024-10-10 07:30:26

【Android】BRVAH多<a href=https://www.elefans.com/category/jswz/34/1770549.html style=布局实现"/>

【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 = 0const val TRACTOR_ITEM_FIVE = 1const val TRACTOR_ITEM_SIX = 2}//使用此构造方法构建第一种布局的数据源constructor(itemType: Int,res: Int,title: String,first: String,second: String,three: String,four: String) : this(itemType) {this.tractorRes = resthis.tractorTitle = titlethis.fourSetFirstName = firstthis.fourSetSecondName = secondthis.fourSetThreeName = threethis.fourSetFourName = four}
//使用此构造方法构建第二种布局的数据源constructor(itemType: Int,res: Int,title: String,first: String,second: String,three: String,four: String,five: String) : this(itemType) {this.tractorRes = resthis.tractorTitle = titlethis.fiveSetFirstName = firstthis.fiveSetSecondName = secondthis.fiveSetThreeName = threethis.fiveSetFourName = fourthis.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 = resthis.tractorTitle = titlethis.sixSetFirstName = firstthis.sixSetSecondName = secondthis.sixSetThreeName = threethis.sixSetFourName = fourthis.sixSetFiveName = fivethis.sixSetSixName = six}
}
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 -> {}
}

使用

//        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

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

更多推荐

【Android】BRVAH多布局实现

本文发布于:2023-12-07 19:32:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1671645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布局   Android   BRVAH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!