admin管理员组文章数量:1336631
I'm filtering items based on user selection and animating them back in a FlowRow that also inside a LazyColumn with keys are unique, however Compose does not remove items from composition, which is also what i need for scroll to not jump, but items that are in composition from previous filter do not have fadeIn animations. Other animations like shrink/expand/slide work fine.
In gif at first filtering item1 and second filtering item3 does not animate fadeIn
This is the ViewModel and filtering, most code is omitted
class SomeViewModel : ViewModel() {
private val list = listOf(
SomeData(id = "1", value = "Row1"),
SomeData(id = "2", value = "Row2"),
SomeData(id = "3", value = "Row3"),
SomeData(id = "4", value = "Row4"),
SomeData(id = "5", value = "Row5")
)
var itemList by mutableStateOf(list)
var filter: Int = 0
fun filter() {
if (filter % 3 == 0) {
itemList = listOf(
list[0],
list[1],
list[2]
)
} else if (filter % 3 == 1) {
itemList = listOf(
list[1],
list[2]
)
} else {
itemList = listOf(
list[0],
list[2],
list[3]
)
}
filter++
}
}
data class SomeData(val id: String, val value: String)
Composable that contains items and animates after filtering takes effect.
@Composable
private fun StaggeredList(
filter: String,
itemList: List<SomeData>,
) {
BoxWithConstraints(
modifier = Modifier
) {
val itemWidth = (maxWidth - 8.dp) / 2
FlowRow(
modifier = Modifier,
maxItemsInEachRow = 2,
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
itemList.forEachIndexed { index, it ->
key(it.id) {
var visible by remember {
mutableStateOf(false)
}
LaunchedEffect(filter) {
visible = false
delay(2000)
visible = true
}
AnimatedVisibility(
visible = visible,
enter = fadeIn(tween(2000)),
exit = fadeOut(tween(2000))
) {
MyRow(
modifier = Modifier.size(itemWidth, 200.dp),
item = it
)
}
}
}
}
}
}
Rest of the code to reproduce the issue
@Preview
@Composable
private fun FlowRowCompositionPreview() {
val viewModel = viewModel<SomeViewModel>()
MyComposable(viewModel)
}
@Composable
fun MyComposable(someViewModel: SomeViewModel) {
Column(modifier = Modifier.fillMaxSize().background(backgroundColor)) {
val itemList = someViewModel.itemList
LazyColumn(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp),
contentPadding = PaddingValues(16.dp)
) {
items(5) {
Box(
modifier = Modifier
.background(Color.Red, RoundedCornerShape(16.dp))
.fillMaxWidth().height(100.dp)
)
}
item {
Button(
modifier = Modifier.padding(16.dp).fillMaxWidth(),
onClick = {
someViewModel.filter()
}
) {
Text("Filter")
}
}
item {
StaggeredList(
filter = someViewModel.filter.toString(),
itemList = itemList
)
}
}
}
}
I'm filtering items based on user selection and animating them back in a FlowRow that also inside a LazyColumn with keys are unique, however Compose does not remove items from composition, which is also what i need for scroll to not jump, but items that are in composition from previous filter do not have fadeIn animations. Other animations like shrink/expand/slide work fine.
In gif at first filtering item1 and second filtering item3 does not animate fadeIn
This is the ViewModel and filtering, most code is omitted
class SomeViewModel : ViewModel() {
private val list = listOf(
SomeData(id = "1", value = "Row1"),
SomeData(id = "2", value = "Row2"),
SomeData(id = "3", value = "Row3"),
SomeData(id = "4", value = "Row4"),
SomeData(id = "5", value = "Row5")
)
var itemList by mutableStateOf(list)
var filter: Int = 0
fun filter() {
if (filter % 3 == 0) {
itemList = listOf(
list[0],
list[1],
list[2]
)
} else if (filter % 3 == 1) {
itemList = listOf(
list[1],
list[2]
)
} else {
itemList = listOf(
list[0],
list[2],
list[3]
)
}
filter++
}
}
data class SomeData(val id: String, val value: String)
Composable that contains items and animates after filtering takes effect.
@Composable
private fun StaggeredList(
filter: String,
itemList: List<SomeData>,
) {
BoxWithConstraints(
modifier = Modifier
) {
val itemWidth = (maxWidth - 8.dp) / 2
FlowRow(
modifier = Modifier,
maxItemsInEachRow = 2,
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
itemList.forEachIndexed { index, it ->
key(it.id) {
var visible by remember {
mutableStateOf(false)
}
LaunchedEffect(filter) {
visible = false
delay(2000)
visible = true
}
AnimatedVisibility(
visible = visible,
enter = fadeIn(tween(2000)),
exit = fadeOut(tween(2000))
) {
MyRow(
modifier = Modifier.size(itemWidth, 200.dp),
item = it
)
}
}
}
}
}
}
Rest of the code to reproduce the issue
@Preview
@Composable
private fun FlowRowCompositionPreview() {
val viewModel = viewModel<SomeViewModel>()
MyComposable(viewModel)
}
@Composable
fun MyComposable(someViewModel: SomeViewModel) {
Column(modifier = Modifier.fillMaxSize().background(backgroundColor)) {
val itemList = someViewModel.itemList
LazyColumn(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp),
contentPadding = PaddingValues(16.dp)
) {
items(5) {
Box(
modifier = Modifier
.background(Color.Red, RoundedCornerShape(16.dp))
.fillMaxWidth().height(100.dp)
)
}
item {
Button(
modifier = Modifier.padding(16.dp).fillMaxWidth(),
onClick = {
someViewModel.filter()
}
) {
Text("Filter")
}
}
item {
StaggeredList(
filter = someViewModel.filter.toString(),
itemList = itemList
)
}
}
}
}
Share
Improve this question
asked Nov 19, 2024 at 16:51
ThracianThracian
67.6k21 gold badges212 silver badges391 bronze badges
Recognized by Mobile Development Collective
0
2 Answers
Reset to default 3Make enter
and exit
animations little shorter than delay
in LaunchedEffect
.
When visibility changes before previous animation has ended, AnimatedVisibility
stops the animation and content changes visibility instantly according to the new state. So when you set visible = true
just before exit
animation has ended, animation is stopped and items appear without enter
animation.
Edit:
Correction - as @Thracian noted in comments, when interrupted, the other animation still plays, but very quickly. The speed of that animation is independent from the stage of the interrupted animation.
AnimatedVisibility
uses Transition
interanlly. In Transition.kt
I found this piece of code:
private fun updateAnimation(
initialValue: T = value,
isInterrupted: Boolean = false,
) {
if (initialValueAnimation?.targetValue == targetValue) {
// This animation didn't change the target value, so let the initial value animation
// take care of it.
animation = TargetBasedAnimation(
interruptionSpec,
typeConverter,
initialValue,
initialValue,
velocityVector.newInstance() // 0 velocity
)
useOnlyInitialValue = true
durationNanos = animation.durationNanos
return
}
val specWithoutDelay =
if (isInterrupted && !isSeeking) {
// When interrupted, use the default spring, unless the spec is also a spring.
if (animationSpec is SpringSpec<*>) animationSpec else interruptionSpec
} else {
animationSpec
}
Which implies that the interruption animation spec is interruptionSpec
, which defined as:
interruptionSpec = spring(visibilityThreshold = visibilityThreshold)
As suggested in Jan Itor's answer setting delay time a bit longer than exit animation lets enter animations run with specified easing. Otherwise enter animations still run but faster as in gif below.
With this snippet
@Composable
private fun StaggeredList(
filter: String,
itemList: List<SomeData>,
) {
BoxWithConstraints(
modifier = Modifier
) {
val itemWidth = (maxWidth - 8.dp) / 2
val heightList by remember {
mutableStateOf(
List(10) { index ->
if (index == 0) {
200.dp
} else Random.nextInt(120, 200).dp
}
)
}
FlowRow(
modifier = Modifier,
maxItemsInEachRow = 2,
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
itemList.forEachIndexed { index, it ->
key(it.id) {
var visible by remember {
mutableStateOf(false)
}
LaunchedEffect(filter) {
visible = false
delay(1500)
visible = true
}
AnimatedVisibility(
visible = visible,
enter = fadeIn(tween(1500)) +
slideInVertically(tween(1500)) {
it / 2
},
exit = fadeOut(tween(1500))
+ slideOutVertically(tween(1500)) {
it / 2
}
) {
MyRow(
modifier = Modifier.size(itemWidth, heightList[index]),
item = it
)
}
}
}
}
}
}
But setting a delay longer in composable causes scrollable LazyColumn to be resized and items to be displayed below. To solve this i retrieved FlowRow height before and set it as height. Also i decided using keys that not only unique to data but also to filtering as well with key(it.id + filter)
@Composable
private fun StaggeredList(
filter: String,
itemList: List<SomeData>,
) {
BoxWithConstraints(
modifier = Modifier
) {
val itemWidth = (maxWidth - 8.dp) / 2
val heightList by remember {
mutableStateOf(
List(10) { index ->
if (index == 0) {
200.dp
} else Random.nextInt(120, 200).dp
}
)
}
var height by remember {
mutableStateOf(0.dp)
}
val density = LocalDensity.current
FlowRow(
modifier = Modifier
.onGloballyPositioned {
val newHeight = it.size.height
if (newHeight != 0) {
height = with(density) {
newHeight.toDp()
}
}
}
.heightIn(min = height)
.border(2.dp, Color.Green),
maxItemsInEachRow = 2,
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
itemList.forEachIndexed { index, it ->
//
本文标签:
版权声明:本文标题:android - AnimatedVisibility fadeIn animations do not work for recomposed items but only for items enter composition? - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1742411631a2469905.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论