使用BaseRecyclerViewAdapterHelper实现树形结构

使用BaseRecyclerViewAdapterHelper实现树形结构

成品图

最后的成品图

先依赖

首先

    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }

然后

 implementation	'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'

数据bean

const val ONE = 1;
const val TWO = 2;
const val THREE = 3;

data class TreeOne(
    var label: String,
    var id: String
): AbstractExpandableItem<TreeTwo>(),MultiItemEntity {
    override fun getItemType(): Int {
        return ONE;
    }
    override fun getLevel(): Int {
        return ONE;
    }
}

data class TreeTwo(
    var label: String,
    var id: String
) : AbstractExpandableItem<TreeThree>(), MultiItemEntity {
    var treeOne:TreeOne? = null
        set(value){
            field= value
        }
        get() = field
    override fun getItemType(): Int {
        return TWO;
    }
    override fun getLevel(): Int {
        return TWO;
    }
}

data class TreeThree(
    var label: String,
    var id: String
): MultiItemEntity {
    var treeTwo:TreeTwo? = null
        set(value){
            field= value
        }
        get() = field
    override fun getItemType(): Int {
        return THREE
    }
}

写adapter

class TreeAdapter(data: MutableList<MultiItemEntity>?) :
    BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {

    init {
        addItemType(ONE, R.layout.level_one)
        addItemType(TWO, R.layout.level_two)
        addItemType(THREE, R.layout.level_three)
    }

    override fun convert(holder: BaseViewHolder?, item: MultiItemEntity?) {
        when (item?.itemType) {
            ONE -> {
                val treeOne = item as TreeOne
                holder?.setText(R.id.label, treeOne.label)
                holder?.itemView?.setOnClickListener {
                    val adapterPosition = holder.adapterPosition
                    if (treeOne.isExpanded) {
                        collapse(adapterPosition)
                    } else {
                        expand(adapterPosition)
                    }
                }
            }
            TWO -> {
                val treeTwo = item as TreeTwo
                holder?.setText(R.id.label, treeTwo.label)
                holder?.itemView?.setOnClickListener {
                    val adapterPosition = holder.adapterPosition
                    if (treeTwo.isExpanded) {
                        collapse(adapterPosition)
                    } else {
                        expand(adapterPosition)
                    }
                }
            }
            THREE -> {
                val TreeThree = item as TreeThree
                holder?.setText(R.id.label, TreeThree.label)
            }
        }
    }
}

在界面中使用

  private var treeList: MutableList<MultiItemEntity> = ArrayList();

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val sampleTree1 = TreeOne("0一级分类1", "111")
        val sampleTree2 = TreeOne("0一级分类2", "112")

        val TreeTwo1 = TreeTwo("1二级分类", "222")
        //二级加三级
        var treeThree = TreeThree("2三级分类312", "333")
        var treeThree2 = TreeThree("2三级分类312", "333")
        treeThree.treeTwo = TreeTwo1
        treeThree2.treeTwo = TreeTwo1
        TreeTwo1.addSubItem(treeThree)
        TreeTwo1.addSubItem(treeThree2)

        val TreeTwo2 = TreeTwo("1二级分类", "222")
        //二级加三级
        TreeTwo2.addSubItem(TreeThree("2三级分类312", "333"))
        TreeTwo2.addSubItem(TreeThree("2三级分类313", "333"))

        //加二级分类
        sampleTree1.addSubItem(TreeTwo1)
        sampleTree1.addSubItem(TreeTwo2)

        treeList.add(sampleTree1)
        treeList.add(sampleTree2)

       	var treeAdapterJa = TreeAdapter(treeList)
		treeAdapterJa.setOnItemClickListener { adapter, view, position ->
            if (treeList[position].itemType==THREE){
               var treeThree:TreeThree = adapter.data[position] as TreeThree
                Log.e(TAG, treeThree.treeTwo?.label!! )
            }
        }
        mBinding?.recyclerView?.layoutManager = LinearLayoutManager(context)
        mBinding?.recyclerView?.adapter = treeAdapterJa
    }

  • 0
    点赞
  • 2
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值