简体   繁体   中英

Manually recompose all AndroidView in JetPack Compose

In my project I use JetPack Compose and the AndroidView to use an XML View.

@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. There is an option in the AndroidView to obverse the state change?

ps: I've simplified MyComposable for the example

You can use the update block.

From the doc :

The update block can be run multiple times (on the UI thread as well) due to recomposition, and it is the right place to set View properties depending on state. When state changes, the block will be reexecuted to set the new properties . Note the block will also be ran once right after the factory block completes

AndroidView(
    factory = { context ->

        TextView(context).apply {
            text = "Initial Value"
        }
    },
    update = {
        it.text =  message
    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM