compose中,为什么修改List元素的属性,LazyColumn不刷新

李国兵*_*李国兵 3 android android-jetpack-compose lazycolumn compose-recomposition

当我修改列表中对象的属性时,UI 不会更新

\n

我的代码\xef\xbc\x9a

\n
    @OptIn(ExperimentalFoundationApi::class)\n    @Composable\n    fun ContactCard(\n    ) {\n        var stateList = remember {\n            mutableStateListOf<ListViewData>()\n        }\n        viewModel!!.recordRespListLiveData!!.observe(this) { it ->\n            it.forEach {\n                stateList.add(ListViewData(false, it))\n            }\n        }\n        LazyColumn() {\n            stateList.forEachIndexed { index, bean ->\n                stickyHeader() {\n                    Box(Modifier.clickable {\n                        stateList[index].visible = true\n                    }) {\n                        ContactNameCard(bean.data.contact, index)\n                    }\n                }\n                items(bean.data.records) { data ->\n                    if (bean.visible) {\n                        RecordItemCard(record = data)\n                    }\n                }\n            }\n        }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

当我单击框时,可见设置为 true,但 RecordItemCard 不显示\xef\xbc\x8c为什么\xef\xbc\x9f

\n

Thr*_*ian 5

要触发 SnapshotList,您需要添加、删除或使用新实例更新现有项目。当前您正在更新现有项目的可见属性。

如果ListViewData是来自数据类的实例,您可以这样做

stateList[index] = stateList[index].copy(visible = true)
Run Code Online (Sandbox Code Playgroud)