当前位置:网站首页>Using baserecyclerviewadapterhelper to implement tree structure
Using baserecyclerviewadapterhelper to implement tree structure
2022-06-12 23:09:00 【And white】
Use BaseRecyclerViewAdapterHelper Implement the tree structure
Finished drawings
Depend on first
First
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
then
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'
data 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
}
}
Write 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)
}
}
}
}
Use... In the interface
private var treeList: MutableList<MultiItemEntity> = ArrayList();
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val sampleTree1 = TreeOne("0 First level classification 1", "111")
val sampleTree2 = TreeOne("0 First level classification 2", "112")
val TreeTwo1 = TreeTwo("1 Secondary classification ", "222")
// Two plus three
var treeThree = TreeThree("2 Three levels of classification 312", "333")
var treeThree2 = TreeThree("2 Three levels of classification 312", "333")
treeThree.treeTwo = TreeTwo1
treeThree2.treeTwo = TreeTwo1
TreeTwo1.addSubItem(treeThree)
TreeTwo1.addSubItem(treeThree2)
val TreeTwo2 = TreeTwo("1 Secondary classification ", "222")
// Two plus three
TreeTwo2.addSubItem(TreeThree("2 Three levels of classification 312", "333"))
TreeTwo2.addSubItem(TreeThree("2 Three levels of classification 313", "333"))
// Add secondary classification
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
}
边栏推荐
- InfoQ geek media's 15th anniversary solicitation | brief introduction to the four challenges of building a micro service architecture
- Market trend report, technical innovation and market forecast of Chinese stump crusher
- Colab教程(超级详细版)及Colab Pro/Colab Pro+使用评测
- Several Tsinghua students I know have left
- 80 lines of code to realize simple rxjs
- lua 条件语句
- MySQL基础篇视图的总结
- 【LeetCode】300. Longest ascending subsequence
- [web technology] 1348- talk about several ways to implement watermarking
- Use js to listen for Keydown event
猜你喜欢
The carrying capacity of L2 level ADAS increased by more than 60% year-on-year in January, and domestic suppliers "emerged"
MySQL row to column, column to row, multiple columns to one row, one row to multiple columns
Insight into China's smart medical industry in 2022
[recommended collection] easy to understand graphic network knowledge - Part 1
Leetcode 890 finding and replacing patterns [map] the leetcode path of heroding
设计消息队列存储消息数据的 MySQL 表格
80 lines of code to realize simple rxjs
模型过拟合-解决方案(二):Dropout
【Web技术】1348- 聊聊水印实现的几种方式
The development trend of digital collections!
随机推荐
度量学习(Metric Learning)【AMSoftmax、Arcface】
认识的几位清华同学都离职了……
Analysis report on the "fourteenth five year plan" and the latest development trend of China's medical information industry from 2022 to 2028
[Part VI] source code analysis and application details of countdownlatch [key]
Go时间格式化 赋值
反走样/抗锯齿技术
项目里面的traceID的设计
CST learning: four element array design of circular patch antenna (II) array formation and combination results
Model over fitting - solution (II): dropout
Design of traceid in the project
Abstract methods and abstract classes
深度学习-神经网络:卷积的实现方法【直接法(精度没损失)、GEMM(矩阵乘法,精度没损失)、FFT(傅里叶变换,精度有损失)、Winograd(精度有损失)】
Several Tsinghua students I know have left
Qrcodejs2 QR code generation JS
管线中的坐标变换
Pytorch中的梯度累加【在实验时,由于GPU显存限制,遇到batch_size不能再增大的情况。为解决该问题,使用梯度累加方法】
Lua conditional statement
Research Report on market supply and demand and strategy of tizanidine industry in China
Research Report on market supply and demand and strategy of China's digital camera lens industry
模型过拟合-解决方案(二):Dropout