Description
@Composable
fun CustomView () {
var selectedItem by remember { mutableIntStateOf(0 ) }
// Adds view to Compose
AndroidView (
modifier = Modifier .fillMaxSize(), // Occupy the max size in the Compose UI tree
factory = { context ->
// Creates view
MyView (context).apply {
// Sets up listeners for View -> Compose communication
setOnClickListener {
selectedItem = 1
}
}
},
update = { view ->
// View's been inflated or state read in this block has been updated
// Add logic here if necessary
// As selectedItem is read here, AndroidView will recompose
// whenever the state changes
// Example of Compose -> View communication
view.selectedItem = selectedItem
}
)
}
@Composable
fun ContentExample () {
Column (Modifier .fillMaxSize()) {
Text (" Look at this CustomView!" )
CustomView ()
}
}
// [START_EXCLUDE silent]
class MyView (context : Context ) : View(context) {
var selectedItem: Int = 0
fun clear () { }
}
// [END_EXCLUDE]
Reactions are currently unavailable
You can’t perform that action at this time.
snippets/compose/snippets/src/main/java/com/example/compose/snippets/interop/InteroperabilityAPIsSnippets.kt
Lines 218 to 260 in 928b461