Simon Stahl
03/21/2022, 6:47 PMSimon Stahl
03/21/2022, 6:47 PM@Composable
fun ListTest() {
var listData by remember { mutableStateOf((0..10).toList()) }
Column {
Button(onClick = {
listData = listData.toMutableList().apply {
removeAt(0)
add(last()+1)
}
}) {
Text(text = "Modify list")
}
LazyRow(
contentPadding = PaddingValues(10.dp),
horizontalArrangement = Arrangement.spacedBy(10.dp)
) {
items(listData) {
ListItem(value = it)
}
}
}
}
@Composable
fun ListItem(value: Int) {
val rememberedItem by remember { mutableStateOf(value) }
Text(
modifier = Modifier
.height(50.dp)
.width(300.dp)
.background(Color.LightGray),
text = "Item $value, remembered: $rememberedItem"
)
}
Simon Stahl
03/21/2022, 6:48 PMSimon Stahl
03/21/2022, 6:49 PMListItem
remembers that value that it shows and then prints the current as well as the remembered values in a textSimon Stahl
03/21/2022, 6:51 PMItem 0, remembered: 0
for the first item, but after one button click it changes to Item 1, remembered: 0
Simon Stahl
03/21/2022, 6:51 PMSimon Stahl
03/21/2022, 6:53 PM1
moves to the first position with the same value after the data change. Is that possible?Simon Stahl
03/21/2022, 6:54 PMremember
would fix the issue as well, but that is not the solution i'm looking forjw
03/21/2022, 6:56 PMitems
content with key(it) { .. }
jw
03/21/2022, 6:59 PMIan Lake
03/21/2022, 7:50 PMitems
has a key
lambda: https://developer.android.com/reference/kotlin/androidx/compose/foundation/lazy/package-summary#(androidx.compose.founda[…]nction1,kotlin.Function2)
a factory of stable and unique keys representing the item. Using the same key for multiple items in the list is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the list will represent the key.The default is null, which doesn't sound correct if you want your state to move between positions
Simon Stahl
03/21/2022, 8:33 PMSimon Stahl
03/21/2022, 8:34 PMkey(it)
method is very interesting too. i was not aware of this and can use it in other placesSimon Stahl
03/21/2022, 8:34 PMColton Idle
03/22/2022, 12:33 AMSimon Stahl
03/22/2022, 12:49 AMColton Idle
03/22/2022, 12:52 AMZach Klippenstein (he/him) [MOD]
03/22/2022, 3:53 PMZach Klippenstein (he/him) [MOD]
03/22/2022, 3:54 PMColton Idle
03/22/2022, 5:23 PMSimon Stahl
03/22/2022, 7:22 PM