提问人:petestmart 提问时间:3/8/2022 更新时间:3/8/2022 访问量:803
可变状态复选框在选择后不更改外观 - Jetpack Compose
Mutable State Checkbox Not Changing Appearance After Selection - Jetpack Compose
问:
这里有一个类似的问题。我尝试了这些解决方案以及其他一些解决方案,但仍然没有取得任何成功。
我在 Jetpack Compose 的对话框中有一个不同选项的清单。选择 optionItem 时,我可以看到状态已更改,但复选框图标不会更新。
val _optionItems by remember {
mutableStateOf(
optionItemArray!!.map {
OptionItem(
label = it.label,
value = it.value,
number = it.number,
type = it.type,
subtitle = it.subtitle,
group = it.group,
matchType = it.matchType,
isSelected = it.isSelected,
isHidden = it.isHidden,
)
}
)
}
val optionItems: MutableList<OptionItem> = _optionItems as MutableList<OptionItem>
fun setOptionSelectedAtIndex(index: Int, isSelected: Boolean){
(_optionItems as MutableList<OptionItem>)[index] = _optionItems[index].copy(isSelected = !isSelected)
}
LazyColumn {
items(optionItems.size) { i ->
Row(
modifier = Modifier
.fillMaxWidth()
.clickable
{
optionItems.mapIndexed { j, item ->
if(i == j){
setOptionSelectedAtIndex(i, item.isSelected)
} else item
}
}
.padding(16.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
optionItems[i].label?.let { Text(text = it) }
if (optionItems[i].isSelected) {
Icon(
Icons.Filled.CheckBox,
contentDescription = "Selected"
)
} else {
Icon(
Icons.Filled.CheckBoxOutlineBlank,
contentDescription = "Not Selected"
)
}
}
}
}
}
我知道应该再次调用该列表,这就是我假设该函数应该做的事情,但我在弄清楚这一点时遇到了很多麻烦。setOptionSelectedAtIndex
答: 暂无答案
评论
(_optionItems as MutableList<OptionItem>)[index] = _optionItems[index].copy(isSelected = !isSelected)
mutableState
MutableList<OptionItem>
mutableStateListOf<OptionItem>
.clickable { optionItems.mapIndexed { j, item -> if(i == j){ setOptionSelectedAtIndex(i, item.isSelected) } else item checkedState.value = !checkedState.value } }