Jorge Domínguez
05/10/2022, 7:51 PMLazyColumn
?
LazyColumn {
tasksList.forEach { task ->
item {
TaskItem(
taskName = task.name,
onStartTask = { onStartTask(task) }
)
}
}
}
I'd like to animate a TaskItem
if the corresponding Task
object is not present in the next tasksList
passed to LazyColumn
Oleksandr Balan
05/10/2022, 7:58 PMtask
has some identifier which you can use for the key?
item(key = ...) {
TaskItem(...)
}
Secondly, you must add an animateItemPlacement
Modifier to each item, like this:
item(key = ...) {
TaskItem(
modifier = Modifier.animateItemPlacement()
)
}
Jorge Domínguez
05/10/2022, 8:09 PManimateItemPlacement()
docs it only animates order changes. Is it possible to animate a visibility change? so that if the item is not present in the new tasksList
then the "disappearance" of the item can be animatedOleksandr Balan
05/10/2022, 8:31 PMJorge Domínguez
05/10/2022, 8:39 PMOleksandr Balan
05/11/2022, 6:13 AMval items = remember { mutableStateListOf(*List(100) { it }.toTypedArray()) }
LazyColumn(
contentPadding = PaddingValues(SpacingMedium),
verticalArrangement = Arrangement.spacedBy(SpacingMedium),
modifier = Modifier.fillMaxSize()
) {
items(items, key = { it }) {
Item(
id = it,
onRemove = { items.remove(it) },
modifier = Modifier
.fillMaxWidth()
.animateItemPlacement()
)
}
}
Jorge Domínguez
05/11/2022, 2:18 PMOleksandr Balan
05/11/2022, 2:28 PMonRemove
creates a new list and sets to the State property:
var items by remember { mutableStateOf(List(100) { it }) }
LazyColumn(
contentPadding = PaddingValues(SpacingMedium),
verticalArrangement = Arrangement.spacedBy(SpacingMedium),
modifier = Modifier.fillMaxSize()
) {
items(items, key = { it }) {
Item(
id = it,
onRemove = { items = items.toMutableList().apply { remove(it) } },
modifier = Modifier
.fillMaxWidth()
.animateItemPlacement()
)
}
}
But yeah, keys should be unique and remain same after change. Alternatively you may use .hashCode()
for your key 🤔Jorge Domínguez
05/11/2022, 2:31 PM.hashCode()
as key, since currently I'm using the primary key field of the Room entity, though it should work the same...