-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic data not working. #14
Comments
Your |
thank you, |
It's okay. Check out the code TreeView/treeview/src/main/kotlin/io/github/dingyi222666/view/treeview/TreeView.kt Line 912 in 1706aca
|
First implement TreeView/app/src/main/kotlin/com/dingyi/treeview/MainActivity.kt Lines 342 to 350 in 1706aca
Set the selection mode to TreeView/app/src/main/kotlin/com/dingyi/treeview/MainActivity.kt Lines 152 to 156 in 1706aca
|
Sorry,I have reviewed your example, and you executed it through the menu item. How do I perform this operation on the switch of a certain node? Thanks class ViewBinder : TreeViewBinder<TreeNode2>(),
TreeNodeEventListener<TreeNode2> {
override fun createView(parent: ViewGroup, viewType: Int): View {
val layoutInflater = LayoutInflater.from(parent.context)
return if (viewType == 1) {
ItemDirBinding.inflate(layoutInflater, parent, false).root
} else {
ItemFileBinding.inflate(layoutInflater, parent, false).root
}
}
override fun getItemViewType(node: TreeNode<TreeNode2>): Int {
if (node.data!!.children.isNotEmpty()) {
return 1
}
return 0
}
override fun bindView(
holder: TreeView.ViewHolder,
node: TreeNode<TreeNode2>,
listener: TreeNodeEventListener<TreeNode2>
) {
if (node.data!!.children.isNotEmpty()) {
applyDir(holder, node)
} else {
applyFile(holder, node)
}
val itemView = holder.itemView.findViewById<Space>(R.id.space)
// (getCheckableView(node, holder) as Switch).apply {
// isVisible = node.selected
// isSelected = node.selected
// }
itemView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
width = node.depth * 22
}
}
override fun getCheckableView(
node: TreeNode<TreeNode2>,
holder: TreeView.ViewHolder
): Checkable? {
return if (node.isChild) {
ItemDirBinding.bind(holder.itemView).switchOn
} else {
ItemFileBinding.bind(holder.itemView).switchOn
}
//return super.getCheckableView(node, holder)
}
private fun applyFile(holder: TreeView.ViewHolder, node: TreeNode<TreeNode2>) {
val binding = ItemFileBinding.bind(holder.itemView)
binding.tvName.text = node.data!!.name.toString()
}
private fun applyDir(holder: TreeView.ViewHolder, node: TreeNode<TreeNode2>) {
val binding = ItemDirBinding.bind(holder.itemView)
binding.tvName.text = node.data!!.name.toString()
binding.switchOn.isChecked = node.data!!.isSwitchOn
binding.switchOn.setOnCheckedChangeListener(object :
CompoundButton.OnCheckedChangeListener {
override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
// node.data!!.isSwitchOn = p1
// if (node.hasChild) {
// flattenTree(node.data!!.children, p1)
// }
/// bad code
lifecycleScope.launch {
binding.treeview.apply {
// select node and it's children
selectionMode = TreeView.SelectionMode.MULTIPLE_WITH_CHILDREN
selectNode(binding.treeview.tree.rootNode, true)
expandAll()
selectionMode = TreeView.SelectionMode.MULTIPLE_WITH_CHILDREN
}
}
}
})
binding
.ivArrow
.animate()
.rotation(if (node.expand) 180f else 0f)
.setDuration(200)
.start()
}
fun flattenTree(treeNodes: List<TreeNode2>, isSwitchOn: Boolean): List<TreeNode2> {
val result = mutableListOf<TreeNode2>()
for (node in treeNodes) {
node.isSwitchOn = isSwitchOn
result.add(node)
if (node.children.isNotEmpty()) {
val flattenedChildren = flattenTree(node.children, isSwitchOn)
result.addAll(flattenedChildren)
}
}
return result
}
// fun flattenTree(): List<TreeNode2> {
// val result = mutableListOf<TreeNode2>()
// for (node in treeNodes) {
// node.isSwitchOn = isSwitchOn
// result.add(node)
// if (node.children.isNotEmpty()) {
// val flattenedChildren = flattenTree(node.children, isSwitchOn)
// result.addAll(flattenedChildren)
// }
// }
// return result
// }
override fun onClick(node: TreeNode<TreeNode2>, holder: TreeView.ViewHolder) {
if (node.data!!.children.isNotEmpty()) {
applyDir(holder, node)
} else {
//Toast.makeText(this@MainActivity, "Clicked ${node.name}", Toast.LENGTH_LONG).show()
}
}
override fun onToggle(
node: TreeNode<TreeNode2>,
isExpand: Boolean,
holder: TreeView.ViewHolder
) {
applyDir(holder, node)
}
}
class ItemTreeNodeGenerator(
private val rootItem: TreeNode2
) : TreeNodeGenerator<TreeNode2> {
suspend fun fetchNodeChildData(targetNode: TreeNode<TreeNode2>): Set<TreeNode2> {
return targetNode.requireData().children.toSet()
}
override fun createNode(
parentNode: TreeNode<TreeNode2>,
currentData: TreeNode2,
tree: AbstractTree<TreeNode2>
): TreeNode<TreeNode2> {
return TreeNode(
data = currentData,
depth = parentNode.depth + 1,
name = currentData.name,
id = tree.generateId(),
hasChild = currentData.children.isNotEmpty(),
// It should be taken from the Item
isChild = currentData.children.isNotEmpty(),
expand = false
)
}
override fun createRootNode(): TreeNode<TreeNode2> {
return TreeNode(
data = rootItem,
// Set to -1 to not show the root node
depth = -1,
name = rootItem.name,
id = Tree.ROOT_NODE_ID,
hasChild = true,
isChild = true
)
}
override suspend fun fetchChildData(targetNode: TreeNode<TreeNode2>): Set<TreeNode2> {
return targetNode.requireData().children.toSet()
}
} |
Upload all your example code to github as a repository, I'll read and modify it later. |
repository: Thanks |
Okay. You can check out my code example: |
Thank you! |
You need to change on both the |
Part of my inspiration comes from here.
#4
It seems to have entered an infinite loop problem.
this is my version
io.github.dingyi222666:treeview:1.3.1
Thanks
ERROR:
The text was updated successfully, but these errors were encountered: