简体   繁体   English

MVVM(数据绑定)中的ViewModel与Android建筑组件中的ViewModel有何不同

[英]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.

相关问题 如何在 Android 中使用 MVVM 数据绑定验证表单 ViewModel? - How to validate form ViewModel with MVVM Data binding in Android? "如何在不绑定到 ViewModel (MVVM) 中的 UI 的情况下使用 android 导航?" - How to use android navigation without binding to UI in ViewModel (MVVM)? 从Android项目到ViewModel的数据绑定 - Data Binding from Android project to ViewModel 如何在MVVM Android中使用数据绑定处理ViewModel中的onClick或onTouch之类的事件 - How to handle onClick or onTouch like events in ViewModel with data binding in MVVM Android 将数据从一个视图模型传递到另一个 android MVVM - Passing data from one viewmodel to another android MVVM 如何使用 viewmodel mvvm 将自定义视图的数据保存在 Android 中? - How to keep Custom View's data in Android using viewmodel mvvm? 如何在Android的MVVM架构中将动作从View发送到ViewModel - How to send actions from View to ViewModel in MVVM architecture in Android Android App 将数据从 ViewModel 传递到 ViewModel - Android App pass data from ViewModel to ViewModel Android MVVM 对话框视图模型 - Android MVVM dialog viewmodel Android MVVM-数据更改时更新ViewModel - Android MVVM - Update ViewModel when data changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM