[英]Best approach to start an Activity from ViewModel in MVVM Architecture
[英]What is the best way to call fragments method from ViewModel in MVVM architecture using live data
例如,由於 ViewModel 應該是松耦合的,我們不能將接口引用傳遞給 ViewModel 以從 Viewmodel 獲取回調並調用片段中實現的方法。
還請提供一個示例或參考,以使用 livedata 調用具有兩個或多個參數的方法。
在 ViewModel 中創建一個實時數據變量並將 Fragment 中的觀察者附加到該變量,每當數據發生變化時,function 將被自動調用。
例如:
viewModel.loading.observe(viewLifecycleOwner, { loading ->
loading?.let {
// Here, I'm calling a new function named setLoaderVisibility
setLoaderVisibility(loader = binding.shimmerProductDetail, binding.containerBody, isLoaderVisible = loading)
}
})
隨意詢問是否有不清楚的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.