[英]How ViewModel in MVVM (data binding) is differ from ViewModel in Android Architectural components
From the link: 从链接:
https://developer.android.com/topic/libraries/architecture/viewmodel.html https://developer.android.com/topic/libraries/architecture/viewmodel.html
I understood that ViewModel(Android Architectural components) is aware of Life cycle of Component. 我了解到ViewModel(Android建筑组件)知道组件的生命周期。
Can we use these two ViewModels together ? 我们可以一起使用这两个ViewModel吗?
Firstly, you need to realize MVVM is the name of an architecture. 首先,您需要意识到MVVM是架构的名称。 It's used to extract the logic from the View and put it into the ViewModel. 它用于从View中提取逻辑并将其放入ViewModel中。 One of the great benefits of doing this is making this logic more easily testable. 这样做的一大好处是使此逻辑更易于测试。
The ViewModel class from the Architecture Components has the same name but is in itself not the same. Architecture Components中的ViewModel类具有相同的名称,但其本身并不相同。 It's just a handy tool to keep data from being deleted when the state of your View changes (eg: when the screen is rotated). 它只是一个方便的工具,可在View状态更改时(例如:旋转屏幕时)防止数据被删除。
As for your last question, the two can of course be used together, but the two are definetely not the same. 至于您的最后一个问题,当然可以将两者一起使用,但是两者在定义上肯定是不相同的。
For more information on implementing MVVM there are some great answers here on StackOverflow already (eg: Android MVVM Design Pattern Examples ). 有关实现MVVM的更多信息,此处已经在StackOverflow上提供了一些不错的答案(例如: Android MVVM设计模式示例 )。
Yes, you can use more than one ViewModel in android application. 是的,您可以在Android应用程序中使用多个ViewModel。 I have created 2 boilerplate projects for newbies references to MVVM architecture 我已经创建了2个样板项目,供初学者参考MVVM体系结构
MVVM with Retrofit- LiveData - ViewModel pattern 具有翻新功能的MVVM-LiveData-ViewModel模式
MVVM with Room database - LiveData-ViewModel pattern 带有Room数据库的MVVM-LiveData-ViewModel模式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.