android - 如何在 Jetpack Compose 中使用小部件?
问题描述
我想使用其中的一些,但我从NumberPicker
which extends开始LinearLayout
,因此不能只在视图中间实例化一个实例。当然,只需要一个嵌入式数字微调器作为输入值的一种方式。
解决方案
您现在可以在 Compose中为尚未为其创建特定可组合项的每个小部件使用Android 视图。
@Composable
fun CustomView() {
val selectedItem = remember { mutableStateOf(0) }
// Adds view to Compose
AndroidView(
modifier = Modifier.fillMaxSize(), // Occupy the max size in the Compose UI tree
factory = { context ->
// Creates custom view
CustomView(context).apply {
// Sets up listeners for View -> Compose communication
myView.setOnClickListener {
selectedItem.value = 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.coordinator.selectedItem = selectedItem.value
}
)
}
推荐阅读
- javascript - 如何将 ExpressJS 路由器传递到隔离虚拟机实例
- python - 如何让驱动程序导航到 selenium python 中的新页面
- javascript - TypeError:无法读取未定义的属性“参数”以更新类别
- flutter - 在 LicensePage 中隐藏我的颤振应用程序中的包
- discord.py - 使用 tatsu API 时对象没有属性
- react-native - React Native:[Branch] customMetadata 值必须是字符串
- python - 如果用户不采取行动功能被省略
- java - 无法在 ftl 文件中映射 jsonRequest 属性
- amazon-web-services - 我正在使用 AWS Cognito 服务,我正在尝试在登录时删除 OTP
- tensorflow - 从 keras 函数转换为 tensorFlow.keras 函数以利用 GPU