我有带有MediatorLiveData的ViewModel。我想像这样合并两个LiveData
private fun mergeData(): LiveData<List<DataIncomeOperations>> {
val summaryLiveData = MediatorLiveData<List<DataIncomeOperations>>()
summaryLiveData.addSource(incomeData) {
summaryLiveData.value = incomeData.value
}
summaryLiveData.addSource(costsData) {
summaryLiveData.value = costsData.value
}
return summaryLiveData
}
val summaryLiveData: LiveData<List<DataIncomeOperations>> = mergeData()
像这样将数据设置为RecyclerView
operationsViewModel.summaryLiveData.observe(
viewLifecycleOwner,
Observer<List<DataOperations>> {
adapter.submitList(it)
})
作为结果,在显示模拟器上显示最后添加的LiveData而不是所有数据 我有点尝试了一切,却找不到解决方法