我有一份地址列表,我希望我的用户仅选择一个送货地址
最佳答案
您只需要跟踪选择的索引即可。
@Composable
fun SingleSelectionList() {
var selectedIndex by remember {
mutableStateOf(-1)
}
LazyColumn(
Modifier
.fillMaxSize()
.selectableGroup() // Optional, for accessibility purpose
) {
items(count = 10) { index ->
Text(
text = "Item $index",
Modifier
.fillMaxWidth()
.selectable(
selected = selectedIndex == index,
onClick = {
selectedIndex = index
}
)
.background(
if (selectedIndex == index) Color.Gray
else Color.Transparent
)
.padding(8.dp)
)
}
}
}
如果您想允许取消选择,请将 onClick
更改为:
selectedIndex = if (selectedIndex == index) -1 else index
关于android - 如何仅选择列表中的一项(LazyColumn)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72531840/