I have read the question Compose LazyColumn select one item.
I think that either Code A or Code B can do almost the same work.
Why should I use the the .selectable modifier instead of .clickable for select one item in Compose LazyColumn ?
Code A
val items = listOf(Pair("A", 1), Pair("AA", 144), Pair("BA", 99))
var selectedItem by remember {mutableStateOf(items[0]) }
LazyColumn {
this.items(items = items) {
Row(modifier = Modifier.clickable{selectedItem = it}) {
if (selectedItem == it) {
Text(it.first, color = Color.Red)
} else {
Text(it.first)
}
}
}
}
Code B
val items = listOf(Pair("A", 1), Pair("AA", 144), Pair("BA", 99))
var selectedItem by remember{mutableStateOf("")}
LazyColumn {
this.items(items = items) {
Row(modifier = Modifier.selectable(
selected = selectedItem == it.first,
onClick = { selectedItem = it.first}
)
) {
if (selectedItem == it.first) {
Text(it.first, color = Color.Red)
} else {
Text(it.first)
}
}
}
}