Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple tree hierarchy rendering with Jetpack Compose
package com.example.myapplication
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.Box
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.ui.tooling.preview.Preview
@ExperimentalAnimationApi
@Composable
fun Node(
modifier: Modifier = Modifier,
nodeModel: NodeModel
) {
Column(
horizontalGravity = Alignment.CenterHorizontally
) {
val isChildrenShown = remember { mutableStateOf(true) }
NodeBox(
modifier = modifier.clickable(onClick = {
isChildrenShown.value = !isChildrenShown.value
}),
isExpanded = isChildrenShown.value
)
Spacer(modifier = Modifier.size(8.dp))
AnimatedVisibility(visible = isChildrenShown.value) {
Row(
modifier = modifier,
horizontalArrangement = Arrangement.Center
) {
nodeModel.children.forEachIndexed { index, model ->
Node(nodeModel = model)
if (index != nodeModel.children.size - 1) {
Spacer(modifier = Modifier.size(4.dp))
}
}
}
}
}
}
@Composable
fun NodeBox(modifier: Modifier = Modifier, isExpanded: Boolean) {
Box(
modifier = modifier.size(8.dp),
backgroundColor = if (isExpanded) Color.Green else Color.Blue
)
}
@ExperimentalAnimationApi
@Preview
@Composable
fun TreePreview() {
Node(
nodeModel = rootModel()
)
}
data class NodeModel(val children: List<NodeModel> = emptyList())
private fun rootModel(): NodeModel {
return NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
)
)
),
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
)
)
),
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment