【发布时间】:2022-11-25 18:00:59
【问题描述】:
我有一个 MutableStateFlow,有点像 emptyList。当我添加项目时,我的视图会毫无问题地重新组合。现在我想在列表清晰时重新组合视图。我尝试了一些代码但没有任何反应。
配对视图模型
class PairViewModel : BaseViewModel() {
val scanLowEnergyDevices by lazy { MutableStateFlow(emptyList<ScanResult>()) }
fun addDevices(result: ScanResult) {
scanLowEnergyDevices.value += result
}
}
内容有状态
@Composable
fun ContentStateful(
context: Context = LocalContext.current,
viewModel: BloodPressurePairViewModel = getViewModel()
) {
val activity = context as ComponentActivity
val scanDeviceList by viewModel.scanLowEnergyDevices.collectAsStateWithLifecycle()
ContentStateLess(
scanDeviceList = scanDeviceList,
resetAction = {
viewModel.scanLowEnergyDevices.value.toMutableList().clear()
}
)
}
ContentStateLess
@Composable
fun ContentStateLess(
scanDeviceList: List<ScanResult>,
resetAction: () -> Unit,
) {
AnimatedVisibility(visible = scanDeviceList.isNotEmpty()) {
Text(text = "scanDeviceList ${scanDeviceList.size}")
Button(onClick = { resetAction() }) {
Text(text = "Reset")
}
}
}
这里有什么问题?谢谢
【问题讨论】:
标签: android kotlin android-jetpack-compose kotlin-flow mutablestateof