Skip to content

Commit

Permalink
docs: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
dingyi222666 committed Jan 14, 2023
1 parent bcc7d42 commit 8b743ff
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 104 deletions.
85 changes: 41 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ An TreeView implement in Android with RecyclerView written in kotlin.
- Introduction Dependency

```groovy
implementation("io.github.dingyi222666:treeview:1.0.2")
implementation("io.github.dingyi222666:treeview:1.0.4")
```

- First, we need a way to get the data to display, in this case we fake some unreal data
Expand Down Expand Up @@ -108,26 +108,25 @@ fun createVirtualFile(): VirtualFile {
- Create a new node generator for the transformation of your data to the nodes

```kotlin

inner class NodeGenerator : TreeNodeGenerator<VirtualFile> {

private val root = createVirtualFile()

override suspend fun refreshNode(
targetNode: TreeNode<VirtualFile>,
oldNodeSet: Set<Int>,
withChild: Boolean,
oldChildNodeSet: Set<Int>,
tree: AbstractTree<VirtualFile>,
): Set<TreeNode<VirtualFile>> = withContext(Dispatchers.IO) {

// delay(100)
val oldNodes = tree.getNodes(oldNodeSet)

val child = checkNotNull(targetNode.extra?.getChild()).toMutableSet()
val oldNodes = tree.getNodes(oldChildNodeSet)

val child = checkNotNull(targetNode.data?.getChild()).toMutableSet()

val result = mutableSetOf<TreeNode<VirtualFile>>()

oldNodes.forEach { node ->
val virtualFile = child.find { it.name == node.extra?.name }
val virtualFile = child.find { it.name == node.data?.name }
if (virtualFile != null) {
result.add(node)
}
Expand All @@ -141,45 +140,43 @@ inner class NodeGenerator : TreeNodeGenerator<VirtualFile> {
child.forEach {
result.add(
TreeNode(
it, targetNode.level + 1, it.name,
tree.generateId(), it.isDir && it.getChild().isNotEmpty(), false
it, targetNode.depth + 1, it.name,
tree.generateId(), it.isDir && it.getChild().isNotEmpty(), it.isDir, false
)
)
}

result
}


override fun createRootNode(): TreeNode<VirtualFile> {
return TreeNode(root, 0, root.name, Tree.ROOT_NODE_ID, true, true)
}


}

```

- Create a node binder to bind the node to the layout, and in most cases also implement node click
events in this class
- Create a node binder to bind the node to the layout, and in most case also implement node click
events in this class

Note: For indenting itemView, we recommend to add a Space to the far left of your layout. The width of this space is the width of the indent.

```kotlin

inner class ViewBinder : TreeViewBinder<VirtualFile>(), TreeNodeListener<VirtualFile> {
inner class ViewBinder : TreeViewBinder<VirtualFile>(), TreeNodeEventListener<VirtualFile> {

override fun createView(parent: ViewGroup, viewType: Int): View {
if (viewType == 1) {
return ItemDirBinding.inflate(layoutInflater, parent, false).root
return if (viewType == 1) {
ItemDirBinding.inflate(layoutInflater, parent, false).root
} else {
return ItemFileBinding.inflate(layoutInflater, parent, false).root
ItemFileBinding.inflate(layoutInflater, parent, false).root
}
}

override fun areContentsTheSame(
oldItem: TreeNode<VirtualFile>,
newItem: TreeNode<VirtualFile>
): Boolean {
return oldItem == newItem && oldItem.extra?.name == newItem.extra?.name
return oldItem == newItem && oldItem.data?.name == newItem.data?.name
}

override fun areItemsTheSame(
Expand All @@ -190,7 +187,7 @@ inner class ViewBinder : TreeViewBinder<VirtualFile>(), TreeNodeListener<Virtual
}

override fun getItemViewType(node: TreeNode<VirtualFile>): Int {
if (node.extra?.isDir == true) {
if (node.data?.isDir == true) {
return 1
}
return 0
Expand All @@ -199,28 +196,28 @@ inner class ViewBinder : TreeViewBinder<VirtualFile>(), TreeNodeListener<Virtual
override fun bindView(
holder: TreeView.ViewHolder,
node: TreeNode<VirtualFile>,
listener: TreeNodeListener<VirtualFile>
listener: TreeNodeEventListener<VirtualFile>
) {
if (node.hasChild) {
applyDir(holder, node)
} else {
applyFile(holder, node)
}


holder.itemView.updatePadding(
top = 0,
right = 0,
bottom = 0,
left = node.level * 10.dp
)

val itemView = if (getItemViewType(node) == 1)
ItemDirBinding.bind(holder.itemView).space
else ItemFileBinding.bind(holder.itemView).space

itemView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
width = node.depth * 10.dp
}
//itemView.updatePadding(top = 0,right = 0, bottom = 0, left = node.level * 10.dp)

}

private fun applyFile(holder: TreeView.ViewHolder, node: TreeNode<VirtualFile>) {
val binding = ItemFileBinding.bind(holder.itemView)
binding.tvName.text = node.name.toString()

}

private fun applyDir(holder: TreeView.ViewHolder, node: TreeNode<VirtualFile>) {
Expand All @@ -231,7 +228,7 @@ inner class ViewBinder : TreeViewBinder<VirtualFile>(), TreeNodeListener<Virtual
.ivArrow
.animate()
.rotation(if (node.expand) 90f else 0f)
.setDuration(100)
.setDuration(0)
.start()
}

Expand All @@ -254,7 +251,6 @@ inner class ViewBinder : TreeViewBinder<VirtualFile>(), TreeNodeListener<Virtual
}
}
}

```

- If you want to implement horizontal scrolling, then you may need to do like this
Expand All @@ -263,23 +259,21 @@ inner class ViewBinder : TreeViewBinder<VirtualFile>(), TreeNodeListener<Virtual
treeview.supportHorizontalScroll = true
```

- Now you can create the tree structure and set up the node generator and node binder for the TreeView, then refresh the data
- Now you can create the tree structure and set up the node generator and node binder for the
TreeView, then refresh the data

```kotlin

val tree = Tree.createTree<VirtualFile>()
val tree = Tree.createTree<VirtualFile>()

tree.generator = NodeGenerator()

tree.initTree()

binding.treeview.apply {
// horizontalScroll support, default is false
(binding.treeview as TreeView<VirtualFile>).apply {
supportHorizontalScroll = true
bindCoroutineScope(lifecycleScope)
this.tree = tree as Tree<Any>
binder = ViewBinder() as TreeViewBinder<Any>
nodeClickListener = binder
this.tree = tree
binder = ViewBinder()
nodeEventListener = binder
}

lifecycleScope.launch {
Expand All @@ -289,6 +283,9 @@ lifecycleScope.launch {

```

- Done! Enjoy using it.

## Special thanks
- [Rosemoe](https://github.com/Rosemoe) (Help improve the Treeview horizontal sliding support)

- [Rosemoe](https://github.com/Rosemoe) (Help improve the TreeView horizontal scrolling support)

29 changes: 15 additions & 14 deletions app/src/main/java/com/dingyi/treeview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import io.github.dingyi222666.view.treeview.AbstractTree
import io.github.dingyi222666.view.treeview.Tree
import io.github.dingyi222666.view.treeview.TreeNode
import io.github.dingyi222666.view.treeview.TreeNodeGenerator
import io.github.dingyi222666.view.treeview.TreeNodeListener
import io.github.dingyi222666.view.treeview.TreeNodeEventListener
import io.github.dingyi222666.view.treeview.TreeView
import io.github.dingyi222666.view.treeview.TreeViewBinder
import kotlinx.coroutines.Dispatchers
Expand All @@ -38,12 +38,12 @@ class MainActivity : AppCompatActivity() {
tree.generator = NodeGenerator()
tree.initTree()

binding.treeview.apply {
(binding.treeview as TreeView<VirtualFile>).apply {
supportHorizontalScroll = true
bindCoroutineScope(lifecycleScope)
this.tree = tree as Tree<Any>
binder = ViewBinder() as TreeViewBinder<Any>
nodeClickListener = binder
this.tree = tree
binder = ViewBinder()
nodeEventListener = binder
}

lifecycleScope.launch {
Expand Down Expand Up @@ -106,13 +106,13 @@ class MainActivity : AppCompatActivity() {
return root
}

inner class ViewBinder : TreeViewBinder<VirtualFile>(), TreeNodeListener<VirtualFile> {
inner class ViewBinder : TreeViewBinder<VirtualFile>(), TreeNodeEventListener<VirtualFile> {

override fun createView(parent: ViewGroup, viewType: Int): View {
if (viewType == 1) {
return ItemDirBinding.inflate(layoutInflater, parent, false).root
return if (viewType == 1) {
ItemDirBinding.inflate(layoutInflater, parent, false).root
} else {
return ItemFileBinding.inflate(layoutInflater, parent, false).root
ItemFileBinding.inflate(layoutInflater, parent, false).root
}
}

Expand Down Expand Up @@ -140,16 +140,18 @@ class MainActivity : AppCompatActivity() {
override fun bindView(
holder: TreeView.ViewHolder,
node: TreeNode<VirtualFile>,
listener: TreeNodeListener<VirtualFile>
listener: TreeNodeEventListener<VirtualFile>
) {
if (node.hasChild) {
applyDir(holder, node)
} else {
applyFile(holder, node)
}

val itemView = if (getItemViewType(node) == 1)
ItemDirBinding.bind(holder.itemView).space
else ItemFileBinding.bind(holder.itemView).space

itemView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
width = node.depth * 10.dp
}
Expand Down Expand Up @@ -197,13 +199,14 @@ class MainActivity : AppCompatActivity() {
inner class NodeGenerator : TreeNodeGenerator<VirtualFile> {

private val root = createVirtualFile()

override suspend fun refreshNode(
targetNode: TreeNode<VirtualFile>,
oldChildNodeSet: Set<Int>,
tree: AbstractTree<VirtualFile>,
): Set<TreeNode<VirtualFile>> = withContext(Dispatchers.IO) {

delay(100)

val oldNodes = tree.getNodes(oldChildNodeSet)

val child = checkNotNull(targetNode.data?.getChild()).toMutableSet()
Expand All @@ -226,7 +229,7 @@ class MainActivity : AppCompatActivity() {
result.add(
TreeNode(
it, targetNode.depth + 1, it.name,
tree.generateId(), it.isDir && it.getChild().isNotEmpty(), false
tree.generateId(), it.isDir && it.getChild().isNotEmpty(), it.isDir, false
)
)
}
Expand All @@ -237,8 +240,6 @@ class MainActivity : AppCompatActivity() {
override fun createRootNode(): TreeNode<VirtualFile> {
return TreeNode(root, 0, root.name, Tree.ROOT_NODE_ID, true, true)
}


}

}
Expand Down
7 changes: 1 addition & 6 deletions app/src/main/res/layout/item_dir.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:paddingTop="10dp"
android:paddingBottom="10dp">

<View
<Space
android:id="@+id/space"
android:layout_width="0dp"
android:layout_height="match_parent" />
Expand All @@ -30,9 +30,4 @@
android:gravity="center_vertical"
android:textSize="18sp"
tools:text="@string/app_name" />

<View
android:id="@+id/spaceRight"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
6 changes: 3 additions & 3 deletions app/src/main/res/layout/item_file.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
android:background="?selectableItemBackground"
android:orientation="horizontal">

<View
<Space
android:id="@+id/space"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="26dp" />
/>

<TextView
android:id="@+id/tv_name"

android:layout_marginLeft="26dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableLeft="@drawable/baseline_insert_drive_file_24"
Expand Down
2 changes: 1 addition & 1 deletion treeview/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mavenPublishing {

signAllPublications()

coordinates("io.github.dingyi222666", "treeview", "1.0.2")
coordinates("io.github.dingyi222666", "treeview", "1.0.4")

pom {
name.set("TreeView")
Expand Down
Loading

0 comments on commit 8b743ff

Please sign in to comment.