mirror of
https://github.com/AdrianKuta/Expandable-RecyclerView.git
synced 2025-07-01 14:58:02 +02:00
Adapter concept
This commit is contained in:
@ -15,7 +15,7 @@ android {
|
||||
minSdkVersion 23
|
||||
targetSdkVersion 29
|
||||
versionCode 1
|
||||
versionName "0.0.1-alpha01"
|
||||
versionName "0.0.1-alpha02"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles 'consumer-rules.pro'
|
||||
@ -35,7 +35,7 @@ dependencies {
|
||||
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||||
implementation 'androidx.core:core-ktx:1.1.0'
|
||||
implementation "androidx.recyclerview:recyclerview:1.1.0"
|
||||
implementation "com.github.adriankuta:tree-structure:1.1.0"
|
||||
implementation "com.github.adriankuta:tree-structure:1.2.0"
|
||||
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
|
@ -1,19 +0,0 @@
|
||||
package com.github.adriankuta.expandable_recyclerview
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.TreeNode
|
||||
|
||||
open class ExpandableGroup<T>(value: T) : TreeNode<T>(value) {
|
||||
|
||||
private var _isExpanded = false
|
||||
val isExpanded: Boolean
|
||||
get() = _isExpanded
|
||||
|
||||
|
||||
open fun expand() {
|
||||
_isExpanded = true
|
||||
}
|
||||
|
||||
open fun collapse() {
|
||||
_isExpanded = false
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package com.github.adriankuta.expandable_recyclerview
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
class ExpandableRecyclerView @JvmOverloads constructor(
|
||||
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
|
||||
) : RecyclerView(context, attrs, defStyleAttr) {
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.github.adriankuta.expandable_recyclerview
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.ChildDeclaration
|
||||
import com.github.adriankuta.datastructure.tree.TreeNode
|
||||
|
||||
class ExpandableTreeNode<T>(value: T) : TreeNode<T>(value) {
|
||||
|
||||
var expanded: Boolean = true
|
||||
set(value) {
|
||||
field = value
|
||||
children.forEach {
|
||||
(it as ExpandableTreeNode).expanded = value
|
||||
}
|
||||
}
|
||||
|
||||
override fun child(value: T, childDeclaration: ChildDeclaration<T>?) {
|
||||
val newChild = ExpandableTreeNode(value)
|
||||
if (childDeclaration != null)
|
||||
newChild.childDeclaration()
|
||||
addChild(newChild)
|
||||
}
|
||||
|
||||
fun getVisibleNodeCount(): Int {
|
||||
return onlyVisibleItems()
|
||||
.size
|
||||
}
|
||||
|
||||
fun getVisibleNode(position: Int): ExpandableTreeNode<T> {
|
||||
return onlyVisibleItems()[position]
|
||||
}
|
||||
|
||||
private fun onlyVisibleItems(): List<ExpandableTreeNode<T>> {
|
||||
//Visible if parent of node is expanded.
|
||||
return map { it as ExpandableTreeNode }
|
||||
.filter { (it.parent as? ExpandableTreeNode)?.expanded ?: true }
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.github.adriankuta.expandable_recyclerview
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.ChildDeclaration
|
||||
|
||||
|
||||
@JvmSynthetic
|
||||
inline fun <reified T> expandableTree(
|
||||
value: T,
|
||||
childDeclaration: ChildDeclaration<T>
|
||||
): ExpandableTreeNode<T> {
|
||||
val treeNode = ExpandableTreeNode(value)
|
||||
treeNode.childDeclaration()
|
||||
return treeNode
|
||||
}
|
@ -3,24 +3,34 @@ package com.github.adriankuta.expandable_recyclerview
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
abstract class MultilevelRecyclerViewAdapter<T, VH: RecyclerView.ViewHolder>: RecyclerView.Adapter<VH>() {
|
||||
abstract class MultilevelRecyclerViewAdapter<T, VH : RecyclerView.ViewHolder> :
|
||||
RecyclerView.Adapter<VH>() {
|
||||
|
||||
abstract fun getExpandableGroups(): ExpandableGroup<T>
|
||||
private lateinit var treeNodes: ExpandableTreeNode<T>
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
|
||||
abstract override fun onCreateViewHolder(parent: ViewGroup, nestLevel: Int): VH
|
||||
|
||||
final override fun onBindViewHolder(holder: VH, position: Int) {
|
||||
onBindViewHolder(holder, treeNodes.getVisibleNode(position))
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
getExpandableGroups().toList()
|
||||
return getExpandableGroups().nodeCount()
|
||||
abstract fun onBindViewHolder(holder: VH, treeNode: ExpandableTreeNode<T>)
|
||||
|
||||
abstract fun getTreeNodes(): ExpandableTreeNode<T>
|
||||
|
||||
final override fun getItemCount(): Int {
|
||||
treeNodes = getTreeNodes()
|
||||
return treeNodes.getVisibleNodeCount()
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: VH, position: Int) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
final override fun getItemViewType(position: Int): Int {
|
||||
|
||||
return treeNodes.getVisibleNode(position).depth()
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return super.getItemViewType(position)
|
||||
fun toggleGroup(expandableTreeNode: ExpandableTreeNode<T>) {
|
||||
expandableTreeNode.expanded = !expandableTreeNode.expanded
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package com.github.adriankuta.expandable_recyclerview;
|
||||
|
||||
public class Temp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ExpandableGroup<String> group = new ExpandableGroup<>("");
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user