Compose 的 "AndroidView" 的工厂方法不记得"remember"值何时更改



我在我的UI上有一个AndroidView,我正在使用工厂范围创建一个自定义视图类。我有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'

如文档中所述,
使用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
}
)

相关内容

  • 没有找到相关文章

最新更新