撰写不会在状态更改时重新组合

问题描述 投票:0回答:1

我有一个底部工作表的撰写 UI,我想在数据更改时更新底部工作表。我正在将状态传递给可组合项,但它不会在数据更改时重新组合。

private var title = MutableLiveData()
    private var subTitle= MutableLiveData()
override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
            setContent {
                MyTheme(themeColorMode = ThemeColorMode.Light) {
                    RelayInfoHalfCard(title = title.observeAsState(), subTitle = subTitle.observeAsState()) {
                        dismiss()
                    }
                }
            }
        }
    }

我的更新功能

fun updateData(title: String, subTitle: String) {
        this.title.value = title
        this.subTitle.value = subTitle
    }

我的撰写代码

@Composable
fun RelayInfoHalfCard(title: State<String>, subTitle:State<String>, onDismiss: () -> Unit){

        HalfCard(topPadding = 20.dp) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(start=10.dp,end=10.dp, bottom = 10.dp)
        ) {
            
            Text(
                text = title.value,
                 modifier = Modifier.padding(start=24.dp,end=24.dp,top=8.dp)
            )
            Text(
                text = subTitle.value,
                modifier = Modifier.padding(start=24.dp,end=24.dp,top=8.dp)
            )
            Button(text = "Got it",Modifier.
            padding(start=20.dp,end=20.dp, bottom = 28.dp,top=36.dp)
                .clickable(onClick = onDismiss))

        }
    }
}

谁能告诉我为什么它不起作用?

android kotlin android-fragments android-jetpack-compose
1个回答
-1
投票

在 Jetpack compose 中,您可能希望使用

remember
来获取可观察数据,而不是
LiveData

这是工作代码片段:-

    fun RelayInfoHalfCard(
        title: String,
        subTitle: String,
        onDismiss: () -> Unit
    ) {

        val title by remember { mutableStateOf("") }
        val subTitle by remember { mutableStateOf("") }

        Card {

            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 10.dp, end = 10.dp, bottom = 10.dp)
            ) {

                Text(
                    text = title,
                    modifier = Modifier.padding(start = 24.dp, end = 24.dp, top = 8.dp)
                )
                Text(
                    text = subTitle,
                    modifier = Modifier.padding(start = 24.dp, end = 24.dp, top = 8.dp)
                )
                Button(
                    onClick = onDismiss, modifier = Modifier.padding(
                        start = 20.dp,
                        end = 20.dp,
                        bottom = 28.dp,
                        top = 36.dp
                    )
                ) { Text("Click me") }
            }
        }
    }

    RelayInfoHalfCard(title = title, subTitle = subTitle)
    { dismiss() }

Compose编译器需要一个

mutableStateOf
remember
进行重组,一旦
mutableStateOf
的状态发生改变就会触发重组

另请参阅this以获取进一步说明。

© www.soinside.com 2019 - 2024. All rights reserved.