Skip to content
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

Added search #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Gapo Tree View
Support TreeView for RecyclerView with highly customizable
Support TreeView for RecyclerView with highly customizable and search support

## Demo
<p float="left">
Expand Down
21 changes: 20 additions & 1 deletion app/src/main/java/com/gapo/treeview/MultiChoiceActivity.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.gapo.treeview

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatCheckBox
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.widget.addTextChangedListener
import com.gg.gapo.treeviewlib.GapoTreeView
import com.gg.gapo.treeviewlib.model.NodeState
import com.gg.gapo.treeviewlib.model.NodeViewData
Expand All @@ -16,6 +19,7 @@ class MultiChoiceActivity : AppCompatActivity(), GapoTreeView.Listener<SampleMod

private lateinit var treeView: GapoTreeView<SampleModel>

@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_multi_choice)
Expand All @@ -26,6 +30,21 @@ class MultiChoiceActivity : AppCompatActivity(), GapoTreeView.Listener<SampleMod
.setListener(this)
.setData(SampleModel.getList().toMutableList())
.build()

findViewById<AppCompatEditText>(R.id.searchEditText)?.addTextChangedListener {
treeView.filter { nodeViewData ->
val searchText = it?.toString()?.lowercase() ?: ""

if (searchText.isBlank())
return@filter true

// if any children match the search, show parents too
val hierarchyList = nodeViewData.getData().getHierarchyNodeChild()
hierarchyList.any {
searchText in it.name.lowercase()
}
}
}
}

override fun onBind(
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/gapo/treeview/SampleModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.gapo.treeview

import android.os.Bundle
import com.gg.gapo.treeviewlib.model.NodeData
import com.gg.gapo.treeviewlib.model.NodeViewData

data class SampleModel(
override val nodeViewId: String,
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/java/com/gapo/treeview/SingleChoiceActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.gapo.treeview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatRadioButton
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.RecyclerView
import androidx.core.widget.addTextChangedListener
import com.gg.gapo.treeviewlib.GapoTreeView
import com.gg.gapo.treeviewlib.model.NodeViewData

Expand All @@ -24,6 +25,21 @@ class SingleChoiceActivity : AppCompatActivity(), GapoTreeView.Listener<SampleMo
.setListener(this)
.setData(SampleModel.getList().toMutableList())
.build()

findViewById<AppCompatEditText>(R.id.searchEditText)?.addTextChangedListener {
treeView.filter { nodeViewData ->
val searchText = it?.toString()?.lowercase() ?: ""

if (searchText.isBlank())
return@filter true

// if any children match the search, show parents too
val hierarchyList = nodeViewData.getData().getHierarchyNodeChild()
hierarchyList.any {
searchText in it.name.lowercase()
}
}
}
}

override fun onBind(
Expand Down
20 changes: 17 additions & 3 deletions app/src/main/res/layout/activity_multi_choice.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tree_view"
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search"
android:padding="16dp" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tree_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</androidx.appcompat.widget.LinearLayoutCompat>
20 changes: 17 additions & 3 deletions app/src/main/res/layout/activity_single_choice.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tree_view"
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search"
android:padding="16dp" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tree_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</androidx.appcompat.widget.LinearLayoutCompat>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">GapoTreeView</string>
<string name="search">Search</string>
</resources>
38 changes: 38 additions & 0 deletions treeview/src/main/java/com/gg/gapo/treeviewlib/GapoTreeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class GapoTreeView<T> private constructor(
adapters: Pair<ConcatAdapter.Config, List<RecyclerView.Adapter<*>>>
) {

private var filterPredicate: (NodeViewData<T>) -> Boolean = { true }
private val nodes = mutableListOf<NodeViewData<T>>()
private val nodesShowOnUI = mutableListOf<NodeViewData<T>>()
private var treeViewAdapter: TreeViewAdapter<T>? = null
Expand Down Expand Up @@ -191,11 +192,13 @@ class GapoTreeView<T> private constructor(
if (isLastNode) {
nodesShowOnUI.addAll(
nodes.filter { it.parentNodeIds.contains(nodeId) && it.nodeLevel == parentNode.nodeLevel + 1 }
.filter(filterPredicate)
)
} else {
nodesShowOnUI.addAll(
min(nodesShowOnUI.size - 1, parentNodeIndex + 1),
nodes.filter { it.parentNodeIds.contains(nodeId) && it.nodeLevel == parentNode.nodeLevel + 1 }
.filter(filterPredicate)
)
}
requestUpdateTree()
Expand Down Expand Up @@ -297,6 +300,41 @@ class GapoTreeView<T> private constructor(
}
}

@Suppress("UNCHECKED_CAST")
fun filter(predicate: (NodeViewData<T>) -> Boolean) {
this.filterPredicate = predicate

val filterNodes = nodes.filter(predicate)

nodesShowOnUI.clear()
nodesShowOnUI.addAll(filterNodes)

// expand all parent nodes
filterNodes.forEach { nodeViewData ->
val parentNodeIds = nodeViewData.parentNodeIds
parentNodeIds.forEach { parentNodeId ->
nodesShowOnUI.find { it.nodeId == parentNodeId }?.isExpanded = true
}

val data = nodeViewData.getData() as NodeData<T>
val hierarchyList = data.getHierarchyNodeChild() as List<NodeData<T>>
if (hierarchyList.size <= 1) {
nodeViewData.isExpanded = false
return@forEach
}

val isContainChildren =
hierarchyList.subList(1, hierarchyList.size).any { hierarchyNodeData ->
nodesShowOnUI.any { it.nodeId == hierarchyNodeData.nodeViewId }
}

nodeViewData.isExpanded = isContainChildren
nodeViewData.isLeaf = !isContainChildren
}

requestUpdateTree()
}

private fun findNodeLevel(node: NodeViewData<T>): Int {
return node.parentNodeIds.size
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ interface NodeData<T> {
fun areContentsTheSame(item: NodeData<T>): Boolean

fun getChangePayload(item: NodeData<T>): Bundle

@Suppress("UNCHECKED_CAST")
fun getHierarchyNodeChild(): List<T> {
return mutableListOf(this as T).apply {
getNodeChild().forEach { this += it.getHierarchyNodeChild() }
}
}
}