当前位置:网站首页>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
}
边栏推荐
- Use js to listen for Keydown event
- 【LeetCode】5. Longest Palindromic Substring
- Common rendering pipeline grooming
- Insight into China's smart medical industry in 2022
- Photoshop:PS如何实现放大图片不模糊
- [leetcode] the k-largest element in the array
- ASP. Net core Middleware
- Colab教程(超级详细版)及Colab Pro/Colab Pro+使用评测
- Design a MySQL table for message queue to store message data
- Anti aliasing / anti aliasing Technology
猜你喜欢

C语言:如何给全局变量起一个别名?

80 lines of code to realize simple rxjs

web3 原则和去中心化

Embedded pipeline out of the box

Mysql concat_ws、concat函数使用
![[web technology] 1348- talk about several ways to implement watermarking](/img/5f/c4f6ba6799202c79d1e9cb7a083952.png)
[web technology] 1348- talk about several ways to implement watermarking

2202-简历制作

MySQL row to column, column to row, multiple columns to one row, one row to multiple columns

Summary of MySQL foundation view

Comprehensive analysis of C array
随机推荐
Report on the "fourteenth five year plan" and strategic strategy recommendations for China's intellectual property protection industry 2022 ~ 2028
ShardingSphere-proxy-5.0.0部署之分表实现(一)
Sword finger offer series - 47 Maximum value of gifts
Embedded pipeline out of the box
Research Report on water sports shoes industry - market status analysis and development prospect forecast
The programmer has worked for 7 years. At the age of 31, he has no choice but to deliver takeout. I really don't want you to go through his journey again
Anti aliasing / anti aliasing Technology
Hostvars in ansible
Analysis report on investment and development trend of gap base of Chinese traditional medicine 2022 ~ 2028
Mysql concat_ WS, concat function use
LeetCode 146. LRU cache
C语言:如何给全局变量起一个别名?
Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation
Leetcode1601: the maximum number of building change requests that can be reached (difficult)
Research Report on market supply and demand and strategy of China's digital camera lens industry
C语言:如何给全局变量起一个别名?
CST learning: four element array design of circular patch antenna (II) array formation and combination results
Design a MySQL table for message queue to store message data
PHP deletes data of the same item in a two-dimensional array
The development trend of digital collections!