为什么我的可组合不重新组合更改 HashMap 的 MutableState 的值? [英] Why my composable not recomposing on changing value for MutableState of HashMap?

查看:38
本文介绍了为什么我的可组合不重新组合更改 HashMap 的 MutableState 的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的可组合不重新组合改变 HashMap 的 MutableState 的值.

Why my composable not recomposing on changing value for MutableState of HashMap.

视图模型

 val imageList: MutableState<HashMap<Int, Uri>> = mutableStateOf(HashMap())
    fun setImage(imageUri: Uri) {
        imageList.value[imagePosition] = imageUri
    }

片段

if (viewModel.imageList.value[stepNo] != null && !TextUtils.isEmpty(
                                            viewModel.imageList.value[stepNo].toString()
                                        )
                                    ) {
                                        Image(
                                            contentDescription = "Recipe",
                                            painter = painterResource(R.drawable.ic_baseline_fastfood_24),
                                            modifier = Modifier
                                                .padding(8.dp)
                                        )
                                    }

fun permissionCallbacks() {
        imagePickerListener =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                if (result.resultCode == Activity.RESULT_OK) {
                    // There are no request codes
                    val data: Intent? = result.data
                    val selectedImageUri = data?.data
                    selectedImageUri?.let { viewModel.setImage(it) }
                }
            }
        requestPermissionLauncher =
            registerForActivityResult(
                ActivityResultContracts.RequestPermission()
            ) { isGranted: Boolean ->
                if (isGranted) {
                    val galleryIntent = Intent(
                        Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                    )
                    galleryIntent.type = "image/*"
                    imagePickerListener?.launch(galleryIntent)
                } else {
                   
                }
            }
    }

我通过调试在 setImage() 方法中正确设置的值看到,但是如果 viewModel.imageList.value[stepNo] 的条件在 hashmap 的值时没有再次调用改变了.

I see by debugging that value correctly set inside setImage() method but if condition for viewModel.imageList.value[stepNo] is not called again when value for hashmap is changed.

推荐答案

mutableStateOf 只能在您将一个值替换为另一个值时进行跟踪.如果你改变当前值的状态,它就无法处理

mutableStateOf can only track when you replace one value with an other one. If you change state of current value no way it can handle it

您可以使用 mutableStateMapOf 而不是 mutableStateOf(HashMap()):

You can use mutableStateMapOf instead of mutableStateOf(HashMap()):

val imageList = mutableStateMapOf<Int, Uri>()
---
fun setImage(imageUri: Uri) {
    imageList[imagePosition] = imageUri
}

这篇关于为什么我的可组合不重新组合更改 HashMap 的 MutableState 的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆