【发布时间】:2023-03-10 06:19:01
【问题描述】:
我试图通过包含下载 ID 和进度值的数据对象列表一次显示多个下载进度条。此对象列表的值正在更新(通过日志记录显示),但 UI 组件在其初始值从 null 更改为第一个进度值后不会更新。请帮忙!
我看到有类似的问题,但他们的解决方案对我不起作用,包括附加观察员。
class DownLoadViewModel() : ViewModel() {
...
private var _progressList = MutableLiveData<MutableList<DownloadObject>>()
val progressList = _progressList // Exposed to the UI.
...
//Update download progress values during download, this is called
// every time the progress updates.
val temp = _progressList.value
temp?.forEach { item ->
if (item.id.equals(download.id)) item.progress = download.progress
}
_progressList.postValue(temp)
...
}
界面组件
@Composable
fun ExampleComposable(downloadViewModel: DownloadViewModel) {
val progressList by courseViewModel.progressList.observeAsState()
val currentProgress = progressList.find { item -> item.id == local.id }
...
LinearProgressIndicator(
progress = currentProgress.progress
)
...
}
【问题讨论】:
标签: android viewmodel android-livedata android-jetpack android-jetpack-compose