英文:
Jetpack compose does not rerender when flow of list changes
问题
以下是您要翻译的代码部分:
我有以下可组合的内容
@Composable
fun ManageCredential(
manageCredentialViewModel: ManageCredentialViewModel
) {
val expandedList by manageCredentialViewModel.expandedList.collectAsState()
val text = "Hello"
Scaffold(
modifier = Modifier.padding(top = 100.dp),
content = { padding ->
Image(
painter = if (expandedList.contains(text)) painterResource(id = R.drawable.drop_down_expand) else painterResource(
id = R.drawable.drop_down_collapse
),
contentDescription = text, modifier = Modifier.padding(padding).clickable {
if (expandedList.contains(text)) {
manageCredentialViewModel.removeFromList(text)
} else {
manageCredentialViewModel.addToList(text)
}
}
)
})
}
视图模型如下
@HiltViewModel
class ManageCredentialViewModel @Inject constructor(
) : ViewModel() {
private val _expandedList = MutableStateFlow<MutableList<String>>(mutableListOf())
val expandedList = _expandedList.asStateFlow()
function addToList(value: String) {
_expandedList.value.add(value)
}
function removeFromList(value: String) {
_expandedList.value.remove(value)
}
}
英文:
I have following composable
@Composable
fun ManageCredential(
manageCredentialViewModel: ManageCredentialViewModel
) {
val expandedList by manageCredentialViewModel.expandedList.collectAsState()
val text = "Hello"
Scaffold(
modifier = Modifier.padding(top = 100.dp),
content = { padding ->
Image(
painter = if (expandedList.contains(text)) painterResource(id = R.drawable.drop_down_expand) else painterResource(
id = R.drawable.drop_down_collapse
),
contentDescription = text, modifier = Modifier.padding(padding).clickable {
if (expandedList.contains(text)) {
manageCredentialViewModel.removeFromList(text)
} else {
manageCredentialViewModel.addToList(text)
}
}
)
})
}
The view model is as follows
@HiltViewModel
class ManageCredentialViewModel @Inject constructor(
) : ViewModel() {
private val _expandedList = MutableStateFlow<MutableList<String>>(mutableListOf())
val expandedList = _expandedList.asStateFlow()
fun addToList(value: String) {
_expandedList.value.add(value)
}
fun removeFromList(value: String) {
_expandedList.value.remove(value)
}
}
I just want to toggle the image if a particular text is added to the list but it never happens
答案1
得分: 1
For recomposition to be triggered you need to change value
of State
. What you currently do is adding or removing items from existing list.
要触发重新组合,您需要更改State
的value
。您当前所做的是向现有列表中添加或删除项目。
You can create SnapshotStateList
with mutableStateListOf
. Any changes in this list by removing, adding or updating an existing item with a new item, easily can be done with data class copy function, you can trigger recomposition and good thing about it is it only triggers recomposition for the Composable that reads that item such as LazyColumn item.
您可以使用mutableStateListOf
创建SnapshotStateList
。通过删除、添加或更新现有项目以及使用数据类复制函数轻松进行的对此列表的任何更改,都可以触发重新组合,其中的好处是它仅会为读取该项的Composable触发重新组合,比如LazyColumn项。
You can refer this answer also:
您还可以参考这个答案:
英文:
For recomposition to be triggered you need to change value
of State
. What you currently do is adding or removing items from existing list.
You can create SnapshotStateList
with mutableStateListOf
. Any changes in this list by removing, adding or updating an existing item with a new item, easily can be done with data class copy function, you can trigger recomposition and good thing about it is it only triggers recomposition for the Composable that reads that item such as LazyColumn item.
You can refer this answer also
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论