Predict the output
The list is A, B, C and row B is expanded. Item A is deleted. Without keys, which row shows as expanded?
@Composable
fun Rows(items: List<String>) {
LazyColumn {
items(items) { item ->
var expanded by remember { mutableStateOf(false) }
Text(item + if (expanded) " (open)" else "",
Modifier.clickable { expanded = !expanded })
}
}
}Hint 1 of 2
Without keys, what identifies a row?
Hint 2 of 2
Which item is at position 1 after the delete?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A C: the open state stays at position 1, and C moves into position 1
Correct. Without keys, state belongs to the position. B moves up to position 0 (closed) and C takes position 1 (open).
Why not the others?
- B B, because remember follows the item. Only with a key does state follow the item.
- C None, because deleting resets all state. State at positions that still exist is kept.
- D B and C both. Each position has one state; only position 1 was open.