我是 Jetpack Compose 的新手,我花了几个小时来寻找如何让 LazyColumn 更新我更新列表的内容。我读过它需要是一个不可变列表才能更新 LazyColumn,但我似乎无法让它工作。
代码如下所示:
@Composable
fun CreateList() {
var myList : List<DailyItem> by remember { mutableStateOf(listOf())}
myList = getDailyItemList() // Returns a List<DailyItem> with latest values and uses mutable list internally
// Function to refresh the list
val onUpdateClick = {
// Do something that updates the list
...
// Get the updated list to trigger a recompose
myList = getDailyItemList()
}
// Create the lazy column
...
}
我已经尝试了几件事,要么是在点击更新按钮时列表永远不会更新,要么只有第一个项目被更新,而不是列表中的其余项目。我查看了文档,上面写着这个,但我不明白:
我们建议您使用可观察的数据持有者,例如 State<List> 和不可变的 listOf(),而不是使用不可观察的可变对象。
如何更新列表以便更新 LazyColumn?