Colton Idle
03/18/2021, 11:13 AM.animateContentSize()
modifier
2. Have the expandable content wrapped in AnimatedVisibility {}
composable
At runtime the behavior of AnimatedVisibility
looks slightly better I think? Code examples of each in thread.Colton Idle
03/18/2021, 11:13 AM@ExperimentalAnimationApi
@Composable
fun ExpandCollapse1(
) {
var visible by remember { mutableStateOf(false) }
Column(
modifier = Modifier.clickable { visible = !visible }.padding(48.dp).animateContentSize()
) {
Row() {
Text("Testing with animatedVisibility")
}
if (visible) {
Column {
Text(
"Testing second line",
modifier = Modifier.padding(bottom = 24.dp),
)
}
}
}
}
@ExperimentalAnimationApi
@Composable
fun ExpandCollapse2(
) {
var visible by remember { mutableStateOf(false) }
Column(modifier = Modifier.clickable { visible = !visible }.padding(48.dp)) {
Row() {
Text("Testing with animatedVisibility")
}
AnimatedVisibility(visible = visible) {
Column {
Text(
"Testing second line",
modifier = Modifier.padding(bottom = 24.dp),
)
}
}
}
}
Timo Drick
03/18/2021, 12:08 PMAnimatedVisibility(
visible = visible,
enter = expandHorizontally(),
exit = shrinkHorizontally()
)
maybe you want expandVertically()Doris Liu
03/18/2021, 5:20 PManimateContentSize
is the minimum effort approach that instantly adds/removes content and animates the container to smooth over the size impact to the parent. AnimatedVisibiity
gives you more control on how the new content appear/disappear with a few more lines.
In some cases the appearing/disappearing content has siblings whose positions are dependent on the size of it. For example, if you have more items below the if(visible)
content in your snippet, I'd recommend using AnimatedVisibility
over animateContentSize
on parent, so that those siblings won't jump to the new position. 🙂Colton Idle
03/18/2021, 10:21 PM