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

nuhkoca

08/04/2022, 8:29 AM
Hello, I set a different background color when a list item is clicked. However when its position changes, a different one gets that background color as the previous item’s position had changed. How do I find a more reliable index then?
Copy code
Box(modifier = Modifier.animateItemPlacement()) {
    InstrumentView(instrument = item, selected = selectedIndex == i) {
        selectedIndex = if (selectedIndex != i) i else -1
    }
}
p

Petr Laštovička

08/04/2022, 3:44 PM
If you are using
LazyColumn
, try setting
items()
parameter key. If normal
Column
, soround your composables in
key() {}
composable. Make sure keys are alway unique
3 Views