https://kotlinlang.org logo
#compose
Title
# compose
v

Vsevolod Ganin

10/19/2020, 11:07 PM
Found a strange behavior in
LazyColumnFor
. When I use positional
remember
inside
itemContent
, it remembers everything for that particular index in the list. So when I remove an item in the source list, the (index + 1) item takes its place and inherits the memory of its predecessor. I think that’s because of the usage of
key(index)
in
LazyColumnFor
implementation. Is this intended?
A small repro
Copy code
@Preview
@Composable
fun Repro() {
    val list = remember { mutableStateListOf("a", "b", "c", "d", "e") }
    LazyColumnForIndexed(items = list) { index, item ->
        val randomNumber = remember { Random.nextInt(0..1000) }
        Text(
            text = "$item: $randomNumber",
            modifier = Modifier.clickable(onClick = {
                list.removeAt(index)
            })
        )
    }
}
Using
key(item)
inside
itemContent
makes it behave as I wanted
z

Zach Klippenstein (he/him) [MOD]

10/19/2020, 11:12 PM
Seems like a bug to me too, that behavior is really surprising. I’d just go ahead and file a bug.
👌 1
v

Vsevolod Ganin

10/19/2020, 11:31 PM
8 Views