Jetpack Compose 在列表的流变化时不重新渲染。

huangapple go评论70阅读模式
英文:

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 = &quot;Hello&quot;

    Scaffold(
        modifier = Modifier.padding(top = 100.dp),
        content = { padding -&gt;
        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&lt;MutableList&lt;String&gt;&gt;(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.

要触发重新组合,您需要更改Statevalue。您当前所做的是向现有列表中添加或删除项目。

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:

您还可以参考这个答案:

https://stackoverflow.com/questions/74699081/jetpack-compose-lazy-column-all-items-recomposes-when-a-single-item-update/74700668#74700668

英文:

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

https://stackoverflow.com/questions/74699081/jetpack-compose-lazy-column-all-items-recomposes-when-a-single-item-update/74700668#74700668

huangapple
  • 本文由 发表于 2023年2月18日 15:10:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定