mirror of
https://github.com/AdrianKuta/Expandable-RecyclerView.git
synced 2025-07-01 14:58:02 +02:00
6: Enhance demo application (#9)
* 6: Use data model instead of String. Remove databinding. * 6: Use online lib dependency * 6: Remove CircleCI
This commit is contained in:
@ -23,9 +23,6 @@ android {
|
||||
buildFeatures {
|
||||
viewBinding true
|
||||
}
|
||||
dataBinding {
|
||||
enabled = true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -34,8 +31,7 @@ dependencies {
|
||||
implementation 'androidx.appcompat:appcompat:1.5.1'
|
||||
implementation 'androidx.core:core-ktx:1.9.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
|
||||
implementation project(path: ':expandable-recyclerview')
|
||||
implementation 'com.github.adriankuta:expandable-recyclerView:2.1.0'
|
||||
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
|
||||
|
@ -3,6 +3,7 @@ package com.github.adriankuta
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isGone
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.github.adriankuta.databinding.ItemLevel1Binding
|
||||
import com.github.adriankuta.databinding.ItemLevel2Binding
|
||||
@ -12,16 +13,16 @@ import com.github.adriankuta.expandable_recyclerview.ExpandableTreeNode
|
||||
import com.github.adriankuta.expandable_recyclerview.expandableTree
|
||||
|
||||
class ExpandableAdapter :
|
||||
ExpandableRecyclerViewAdapter<String, ExpandableAdapter.ExpandableViewHolder>() {
|
||||
ExpandableRecyclerViewAdapter<Region, ExpandableAdapter.ExpandableViewHolder>() {
|
||||
|
||||
private var tree: ExpandableTreeNode<String>? = null
|
||||
private var tree: ExpandableTreeNode<Region>? = null
|
||||
|
||||
fun setTree(tree: ExpandableTreeNode<String>) {
|
||||
fun setTree(tree: ExpandableTreeNode<Region>) {
|
||||
this.tree = tree
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun getTreeNodes(): ExpandableTreeNode<String> = tree ?: expandableTree("")
|
||||
override fun getTreeNodes(): ExpandableTreeNode<Region> = tree ?: expandableTree(Region(""))
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, nestLevel: Int): ExpandableViewHolder {
|
||||
return when (nestLevel) {
|
||||
@ -34,7 +35,7 @@ class ExpandableAdapter :
|
||||
|
||||
override fun onBindViewHolder(
|
||||
holder: ExpandableViewHolder,
|
||||
treeNode: ExpandableTreeNode<String>,
|
||||
treeNode: ExpandableTreeNode<Region>,
|
||||
nestLevel: Int
|
||||
) {
|
||||
holder.bind(treeNode) {
|
||||
@ -46,41 +47,65 @@ class ExpandableAdapter :
|
||||
|
||||
class Level1(private val binding: ItemLevel1Binding) : ExpandableViewHolder(binding.root) {
|
||||
override fun bind(
|
||||
node: ExpandableTreeNode<String>,
|
||||
onClickListener: ((ExpandableTreeNode<String>) -> Unit)?
|
||||
node: ExpandableTreeNode<Region>,
|
||||
onClickListener: ((ExpandableTreeNode<Region>) -> Unit)?
|
||||
) {
|
||||
binding.node = node
|
||||
binding.root.setOnClickListener { onClickListener?.invoke(node) }
|
||||
binding.executePendingBindings()
|
||||
binding.apply {
|
||||
root.setOnClickListener { onClickListener?.invoke(node) }
|
||||
textView.text = node.value.name
|
||||
expandIcon.isGone = node.children.isEmpty()
|
||||
expandIcon.setImageResource(
|
||||
if (node.expanded)
|
||||
R.drawable.ic_expand_less_black_24dp
|
||||
else
|
||||
R.drawable.ic_expand_more_black_24dp
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Level2(private val binding: ItemLevel2Binding) : ExpandableViewHolder(binding.root) {
|
||||
override fun bind(
|
||||
node: ExpandableTreeNode<String>,
|
||||
onClickListener: ((ExpandableTreeNode<String>) -> Unit)?
|
||||
node: ExpandableTreeNode<Region>,
|
||||
onClickListener: ((ExpandableTreeNode<Region>) -> Unit)?
|
||||
) {
|
||||
binding.node = node
|
||||
binding.root.setOnClickListener { onClickListener?.invoke(node) }
|
||||
binding.executePendingBindings()
|
||||
binding.apply {
|
||||
root.setOnClickListener { onClickListener?.invoke(node) }
|
||||
textView.text = node.value.name
|
||||
expandIcon.isGone = node.children.isEmpty()
|
||||
expandIcon.setImageResource(
|
||||
if (node.expanded)
|
||||
R.drawable.ic_expand_less_black_24dp
|
||||
else
|
||||
R.drawable.ic_expand_more_black_24dp
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Level3(private val binding: ItemLevel3Binding) : ExpandableViewHolder(binding.root) {
|
||||
override fun bind(
|
||||
node: ExpandableTreeNode<String>,
|
||||
onClickListener: ((ExpandableTreeNode<String>) -> Unit)?
|
||||
node: ExpandableTreeNode<Region>,
|
||||
onClickListener: ((ExpandableTreeNode<Region>) -> Unit)?
|
||||
) {
|
||||
binding.node = node
|
||||
binding.root.setOnClickListener { onClickListener?.invoke(node) }
|
||||
binding.executePendingBindings()
|
||||
binding.apply {
|
||||
root.setOnClickListener { onClickListener?.invoke(node) }
|
||||
textView.text = node.value.name
|
||||
expandIcon.isGone = node.children.isEmpty()
|
||||
expandIcon.setImageResource(
|
||||
if (node.expanded)
|
||||
R.drawable.ic_expand_less_black_24dp
|
||||
else
|
||||
R.drawable.ic_expand_more_black_24dp
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
abstract fun bind(
|
||||
node: ExpandableTreeNode<String>,
|
||||
onClickListener: ((ExpandableTreeNode<String>) -> Unit)? = null
|
||||
node: ExpandableTreeNode<Region>,
|
||||
onClickListener: ((ExpandableTreeNode<Region>) -> Unit)? = null
|
||||
)
|
||||
|
||||
}
|
||||
|
@ -9,23 +9,24 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
val tree = expandableTree("World") {
|
||||
child("North America") {
|
||||
child("USA")
|
||||
val tree = expandableTree(Region("World")) {
|
||||
child(Region("North America")) {
|
||||
child(Region("USA"))
|
||||
}
|
||||
child("Europe") {
|
||||
child("Poland") {
|
||||
child("Warsaw")
|
||||
child(Region("Europe")) {
|
||||
child(Region("Poland")) {
|
||||
child(Region("Warsaw"))
|
||||
}
|
||||
child("Germany")
|
||||
child(Region("Germany"))
|
||||
}
|
||||
child("Asia") {
|
||||
child("China")
|
||||
child(Region("Asia")) {
|
||||
child(Region("China"))
|
||||
}
|
||||
}
|
||||
|
||||
|
3
app/src/main/java/com/github/adriankuta/Region.kt
Normal file
3
app/src/main/java/com/github/adriankuta/Region.kt
Normal file
@ -0,0 +1,3 @@
|
||||
package com.github.adriankuta
|
||||
|
||||
data class Region(val name: String)
|
@ -1,44 +1,31 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<data>
|
||||
<ImageView
|
||||
android:id="@+id/expand_icon"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="0dp"
|
||||
android:scaleType="centerInside"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toBottomOf="@id/textView"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/textView"
|
||||
tools:src="@drawable/ic_expand_less_black_24dp"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<variable
|
||||
name="node"
|
||||
type="com.github.adriankuta.expandable_recyclerview.ExpandableTreeNode<String>" />
|
||||
|
||||
<import type="android.view.View" />
|
||||
</data>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
tools:viewBindingIgnore="true">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/expand_icon"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="0dp"
|
||||
android:scaleType="centerInside"
|
||||
android:src="@{node.expanded ? @drawable/ic_expand_more_black_24dp : @drawable/ic_expand_less_black_24dp}"
|
||||
android:visibility="@{node.children.empty ? View.GONE : View.VISIBLE}"
|
||||
app:layout_constraintBottom_toBottomOf="@id/textView"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/textView"
|
||||
tools:src="@drawable/ic_expand_less_black_24dp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="8dp"
|
||||
android:text="@{node.value}"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@id/expand_icon"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:text="@tools:sample/full_names" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</layout>
|
||||
android:padding="8dp"
|
||||
android:text="@{node.value}"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@id/expand_icon"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:text="@tools:sample/full_names" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
@ -1,46 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<data>
|
||||
<ImageView
|
||||
android:id="@+id/expand_icon"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginStart="24dp"
|
||||
android:scaleType="centerInside"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toBottomOf="@id/textView"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/textView"
|
||||
tools:src="@drawable/ic_expand_less_black_24dp"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<variable
|
||||
name="node"
|
||||
type="com.github.adriankuta.expandable_recyclerview.ExpandableTreeNode<String>" />
|
||||
|
||||
<import type="android.view.View" />
|
||||
</data>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
tools:viewBindingIgnore="true">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/expand_icon"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginStart="24dp"
|
||||
android:scaleType="centerInside"
|
||||
android:src="@{node.expanded ? @drawable/ic_expand_more_black_24dp : @drawable/ic_expand_less_black_24dp}"
|
||||
android:visibility="@{node.children.empty ? View.GONE : View.VISIBLE}"
|
||||
app:layout_constraintBottom_toBottomOf="@id/textView"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/textView"
|
||||
tools:src="@drawable/ic_expand_less_black_24dp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="8dp"
|
||||
android:text="@{node.value}"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@id/expand_icon"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_goneMarginStart="48dp"
|
||||
tools:text="@tools:sample/full_names" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</layout>
|
||||
android:padding="8dp"
|
||||
android:text="@{node.value}"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@id/expand_icon"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_goneMarginStart="48dp"
|
||||
tools:text="@tools:sample/full_names" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
@ -1,46 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<data>
|
||||
<ImageView
|
||||
android:id="@+id/expand_icon"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginStart="48dp"
|
||||
android:scaleType="centerInside"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toBottomOf="@id/textView"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/textView"
|
||||
tools:src="@drawable/ic_expand_less_black_24dp"
|
||||
tools:visibility="visible"/>
|
||||
|
||||
<variable
|
||||
name="node"
|
||||
type="com.github.adriankuta.expandable_recyclerview.ExpandableTreeNode<String>" />
|
||||
|
||||
<import type="android.view.View" />
|
||||
</data>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
tools:viewBindingIgnore="true">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/expand_icon"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginStart="48dp"
|
||||
android:scaleType="centerInside"
|
||||
android:src="@{node.expanded ? @drawable/ic_expand_more_black_24dp : @drawable/ic_expand_less_black_24dp}"
|
||||
android:visibility="@{node.children.empty ? View.GONE : View.VISIBLE}"
|
||||
app:layout_constraintBottom_toBottomOf="@id/textView"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/textView"
|
||||
tools:src="@drawable/ic_expand_less_black_24dp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="8dp"
|
||||
android:text="@{node.value}"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@id/expand_icon"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_goneMarginStart="96dp"
|
||||
tools:text="@tools:sample/full_names" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</layout>
|
||||
android:padding="8dp"
|
||||
android:text="@{node.value}"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@id/expand_icon"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_goneMarginStart="96dp"
|
||||
tools:text="@tools:sample/full_names" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
Reference in New Issue
Block a user