[英]Manually recompose all AndroidView in JetPack Compose
In my project I use JetPack Compose and the AndroidView to use an XML View.在我的项目中,我使用 JetPack Compose 和 AndroidView 来使用 XML 视图。
@Composable
fun MyComposable(
message: String
) {
AndroidView(
factory = { context ->
TextView(context).apply {
text = message
}
})
}
My issue is that when my message
state change, the XML view in the AndroidView
isn't recomposed.我的问题是,当我的
message
state 更改时, AndroidView
中的 XML 视图不会重新组合。 There is an option in the AndroidView
to obverse the state change? AndroidView
中有一个选项可以阻止 state 更改?
ps: I've simplified MyComposable
for the example ps:我已经为示例简化了
MyComposable
You can use the update
block.您可以使用
update
块。
The
update
block can be run multiple times (on the UI thread as well) due to recomposition, and it is the right place to setView
properties depending on state.由于重组,
update
块可以多次运行(也可以在 UI 线程上),它是根据 state 设置View
属性的正确位置。 When state changes, the block will be reexecuted to set the new properties .当 state 更改时,将重新执行该块以设置新属性。 Note the block will also be ran once right after the
factory
block completes请注意,该块也将在
factory
块完成后立即运行一次
AndroidView(
factory = { context ->
TextView(context).apply {
text = "Initial Value"
}
},
update = {
it.text = message
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.