简体   繁体   English

android - view 和 viewModel 之间的 1:n 关系是什么

[英]android - What is the 1:n relationship between view and viewModel

I know view and viewmodel are 1:n.我知道视图和视图模型是 1:n。 However, in most examples there was one viewmodel per activity.但是,在大多数示例中,每个活动都有一个视图模型。 ex) mainActivity - mainViewModel, userListActivity - userListViewModel例如)mainActivity - mainViewModel、userListActivity - userListViewModel

Shouldn't it be like below if it's a 1:n relationship??如果是 1:n 关系,不应该像下面这样吗? ex) mainActivity - viewModel, userListActivity - viewModel例如)mainActivity - viewModel、userListActivity - viewModel

What does 1:n mean? 1:n 是什么意思? Does it mean n activities in one viewmodel?这是否意味着一个视图模型中有 n 个活动? Does it mean n xml files in one viewmodel?这是否意味着一个视图模型中有 n 个 xml 文件?

Yes the common use-case is 1:1 .是的,常见的用例是1:1 But theoretically you could have multiple ViewModels serving a view, so that's why the 1:n relation.但理论上你可以有多个ViewModel服务于一个视图,所以这就是1:n关系的原因。

In practice this could be useful if you'd like to share a ViewModel between two views for example, or when you have a shared Flow ViewModel实际上,如果您想在两个视图之间共享ViewModel ,或者当您拥有共享的Flow ViewModel时,这可能会很有用

So basically in that flow every View could communicate with two ViewModels : the shared one and one specific for that view:因此,基本上在该流程中,每个View都可以与两个ViewModel通信:共享一个和一个特定于该视图:

  • Imagine a SignUp flow, with two screens user credentials (email, password, etc...) and another one with some preferences (like membership, notification, address or other app specific settings)想象一下一个SignUp流程,有两个屏幕用户凭据(电子邮件、密码等),另一个屏幕有一些偏好(如会员资格、通知、地址或其他应用程序特定设置)
  • In this case the SharedFlowViewModel could hold all the user information, and at the last screen the Submit will send the request.在这种情况下, SharedFlowViewModel可以保存所有用户信息,并且在最后一个屏幕上, Submit将发送请求。 All other screens with the individual ViewModel s would just handle the individual screen logic: form validation, input enabling/desabling, etc...具有单个ViewModel的所有其他屏幕将只处理单个屏幕逻辑:表单验证、输入启用/禁用等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 扩展BaseObservable的ViewModel和Android ViewModel Class有什么区别? - What is the difference between ViewModel that extends BaseObservable and Android ViewModel Class? SEAndroid策略和Android权限之间是什么关系? - what is the relationship between SEAndroid policy and Android permissions? Android与lua和SMALI语言之间是什么关系? - what is relationship between Android and lua and SMALI languages? Android中XML和Java之间的关系是什么? - What is the relationship between XML and Java in Android? Android中canvas和matrix之间的关系是什么? - What is the relationship between canvas and matrix in Android? Android中Looper、Handler和MessageQueue是什么关系? - What is the relationship between Looper, Handler and MessageQueue in Android? 用于自定义视图的 Android ViewModel - Android ViewModel for a custom view Android koin 中的注入与视图模型有什么区别 - What is difference between in inject vs viewmodel in Android koin Android相机中的screenSize,previewSize和pictureSize之间是什么关系? - What is the relationship between screenSize, previewSize and pictureSize in Android camera? 各种Android OpenGL相关类之间的关系是什么? - What is the relationship between the various Android OpenGL-related classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM