当 "remember" 值更改时,Compose 的 "AndroidView" 的工厂方法不会重新调用
Compose's "AndroidView"'s factory method doesn't recall when "remember" value change
我的 UI 上有一个 AndroidView
,我正在使用工厂范围创建自定义视图 class。我的 remember
值高于我的 android 视图,该值随用户操作而改变。
val isActive = remember { mutableStateOf(true) }
AndroidView(
modifier = Modifier
.align(Alignment.CenterStart)
.fillMaxWidth()
.wrapContentHeight(),
factory = {
.....
if (isActive) {
...... // DOESN'T RECALL
}
.......
CustomViewClass().rootView
})
if (isActive) {
//WORKS FINE WHEN VALUE CHANGE
} else {
//WORKS FINE WHEN VALUE CHANGE
}
在工厂范围内,我尝试使用 isActive
值来配置 AndroidView,但当 isActive
值更改时它不会触发。
在工厂范围之外,一切正常。
有什么方法可以通知视图或任何解决方法吗?
compose_version = '1.1.1'
如 Docs、
使用 update
来处理状态变化。
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
}
完整代码
// 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
}
)
我的 UI 上有一个 AndroidView
,我正在使用工厂范围创建自定义视图 class。我的 remember
值高于我的 android 视图,该值随用户操作而改变。
val isActive = remember { mutableStateOf(true) }
AndroidView(
modifier = Modifier
.align(Alignment.CenterStart)
.fillMaxWidth()
.wrapContentHeight(),
factory = {
.....
if (isActive) {
...... // DOESN'T RECALL
}
.......
CustomViewClass().rootView
})
if (isActive) {
//WORKS FINE WHEN VALUE CHANGE
} else {
//WORKS FINE WHEN VALUE CHANGE
}
在工厂范围内,我尝试使用 isActive
值来配置 AndroidView,但当 isActive
值更改时它不会触发。
在工厂范围之外,一切正常。
有什么方法可以通知视图或任何解决方法吗?
compose_version = '1.1.1'
如 Docs、
使用 update
来处理状态变化。
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
}
完整代码
// 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
}
)