Jetpack Compose MutableLiveData 不更新 UI 组件 [英] Jetpack Compose MutableLiveData not updating UI Components
问题描述
我试图通过包含下载 ID 和进度值的数据对象列表一次显示多个下载进度条.此对象列表的值正在正常更新(通过日志显示),但 UI 组件的初始值从 null 更改为第一个进度值后不会更新.请帮忙!
我看到有类似的问题,但他们的解决方案对我不起作用,包括附加观察员.
class DownLoadViewModel() : ViewModel() {...private var _progressList = MutableLiveData>()val progressList = _progressList//暴露给 UI....//在下载过程中更新下载进度值,这被称为//每次进度更新时.val temp = progress.valuetemp?.forEach { item ->if (item.id.equals(download.id)) item.progress = download.progress}进度.postValue(温度)...}
界面组件
@Composable有趣的 ExampleComposable(downloadViewModel: DownloadViewModel) {val progressList by courseViewModel.progressList.observeAsState()val currentProgress = progressList.find { item ->item.id == local.id }...线性进度指示器(进度 = currentProgress.progress)...}
我搜索了很多文字,解决了ViewModel中的List没有更新Composable的问题.我试了三种方式都没有用,比如:LiveData、MutableLiveData、mutableStateListOf、MutableStateFlow
根据测试,我发现值变了,但是界面没有更新.文档说只有当 State 的值发生变化时,页面才会更新.根本问题是数据问题.如果没有更新,说明State没有监控到数据更新.
以上方法对增删都有效,但是单独更新不行,因为我更新了T中的元素,但是对象没有变化.
解决方案是深拷贝.
<预><代码>有趣的同意Greet(问候:问候){val g = greet.copy(agree = true)//这种方式无效收藏夹[0] = g}<预><代码>有趣的同意Greet(问候:问候){val g = greet.copy()//这种方式有效g.同意 = 真收藏夹[0] = g}
很奇怪,浪费了很多时间,希望对需要更新的人有所帮助.
I am trying to display several download progress bars at once via a list of data objects containing the download ID and the progress value. The values of this list of objects is being updated fine (shown via logging) but the UI components WILL NOT update after their initial value change from null to the first progress value. Please help!
I see there are similar questions to this, but their solutions are not working for me, including attaching an observer.
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 = progress.value
temp?.forEach { item ->
if (item.id.equals(download.id)) item.progress = download.progress
}
progress.postValue(temp)
...
}
UI Component
@Composable
fun ExampleComposable(downloadViewModel: DownloadViewModel) {
val progressList by courseViewModel.progressList.observeAsState()
val currentProgress = progressList.find { item -> item.id == local.id }
...
LinearProgressIndicator(
progress = currentProgress.progress
)
...
}
I searched a lot of text to solve the problem that List in ViewModel does not update Composable. I tried three ways to no avail, such as: LiveData, MutableLiveData, mutableStateListOf, MutableStateFlow
According to the test, I found that the value has changed, but the interface is not updated. The document says that the page will only be updated when the value of State changes. The fundamental problem is the data problem. If it is not updated, it means that State has not monitored the data update.
The above methods are effective for adding and deleting, but the alone update does not work, because I update the element in T, but the object has not changed.
The solution is to deep copy.
fun agreeGreet(greet: Greet) {
val g = greet.copy(agree = true) // This way is invalid
favourites[0] = g
}
fun agreeGreet(greet: Greet) {
val g = greet.copy() // This way works
g.agree = true
favourites[0] = g
}
Very weird, wasted a lot of time, I hope it will be helpful to those who need to update.
这篇关于Jetpack Compose MutableLiveData 不更新 UI 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!