片段的ViewModel而不是访问Activity ViewModel? [英] ViewModel for Fragment instead accessing Activity ViewModel?

查看:445
本文介绍了片段的ViewModel而不是访问Activity ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单.问题在于使用ViewModels,LiveData和其他相关的 Lifecycle 有意识的拱门方法.
我有一个带有NavDrawer的Activity,可以在其中切换片段.
还有一种情况是屏幕上同时出现两个片段-这将是主要的痛苦. 一个片段的ViewPager带有嵌套的Fragments(不要问为什么). 另一个片段只是在用户执行某些操作时从第一个片段获取信息.仅通过共享活动视图模型即可实现.但是该应用程序本身具有许多业务逻辑,并且随着它的发展,视图模型变得越来越大.
我想问的-不是收据或规则,如何解决此问题,或者可能如何通过修复项目的整个结构来克服此问题.我想征求建议,如何在android.arch.lifecycle样式中应用MVVM方法来挖掘用例.
我没有看到更复杂的东西,而只是在Fragments之间共享Activity ViewModel.但是常见的是,这不是治愈方法.

The problem is quite straightforward. The question is in context of using ViewModels, LiveData and other related Lifecycle aware arch approaches.
I have an Activity with NavDrawer, which switches fragments inside.
And also I have a case when two fragments are present at the same time on the screen - this will be the main pain. One Fragment has a ViewPager with nested Fragments(don't ask why). The other fragment is just obtaining info from first one when user performs some actions. This is achieved just by sharing activity viewmodel. But the app itself has a lot of business logic and as it goes further the viewmodel goes bigger and bigger.
What I want to ask - not a receipt or rules how to fix this, or maybe how to overcome this by fixing the entire structure of the project. I want to ask for suggestions how can I apply the MVVM approach within android.arch.lifecycle style to mine use-case.
I haven't seen something more complicated then just sharing the Activity ViewModel between Fragments. But common, that's not a cure.

您在这里可以看到的-实际上是一团糟.关键是所有人都共享ActivityViewModel. FirstFragment的连接(聚合)表示FirstFragment中的ViewPager正在启动ChildFragments,并且它们也与同一个ActivityViewModel一起工作(杀死我).因此,每个人都在使用一个共享的ViewModel.
我的建议是为每个图层添加一个ViewModel.因此,Activity/Fragments/ChildFragments具有自己的ViewModel. 但是,这里出现了什么- 我们应该如何沟通 ?
可能的解决方案:

What you can see here - a mess actually. The point is that all are sharing the ActivityViewModel. Connections(aggregation) from FirstFragment mean that ViewPager inside FirstFragment is initiating ChildFragments and they are also working with the same ActivityViewModel(kill me). So as result everyone is working with one shared ViewModel.
My proposal is to add a ViewModel for each Layer. So that Activity/Fragments/ChildFragments have their own ViewModels. But what appears here - how we should communicate then?
Possible solutions :

  • 每个组件具有两个ViewModel.一个ViewModel将处理/委托业务逻辑,另一个将进行通信. 每个组件两个视图模型-不太好吗?
  • 具有旧式界面(请不要!)
  • 其他解决方法-例如DB/SharedPrefs/Realm更改侦听器和事件总线(我对此太老了:().

  • Having two ViewModels per one component. One ViewModel will handle/delegate the business logic and another will make the communication. Two viewmodels per component - not so good, yeah?
  • Having old manner interface(please no!)
  • Other workarounds - like DB/SharedPrefs/Realm change listeners and Event Buses(I'm too old for this :( ).

您的解决方案在这里!

我要说的是,以上所有内容都违反了许多设计原则,那么我该怎么办? 我该如何摆脱这个混乱局面?是否有任何Uncle Bob或其他superhero可以帮助您?

I'll say that all of the above are breaking a lot of design principles, so what should I do? How should I come out of this mess? Is there any Uncle Bob or another superhero here to help?

PS -好吧,创建UML或其他图表并不是我的专长.抱歉
PPS -我知道 google <一个href ="https://github.com/googlesamples/android-architecture" rel ="noreferrer">样本.

P.S. - Well, creating UMLs or other charts isn't mine forte. Sorry for that.
P.P.S. - I'm aware of google samples.

推荐答案

首先,将多个 ViewModel用于单个 查看.

我会考虑我的 ViewModel 是什么样的数据正在获取和操作,并以某种方式将它们分组,这看起来很自然.

I would think about my ViewModel's like what kind of data is getting and manipulating, and group them in a way, that seems natural.

对于您的情况,如果片段和活动的逻辑非常相似,我认为您可以使用一个ViewModel,但是我会避免这种情况.

For your case, if the fragments and the activity's logic is very similar, I think you can go with a single ViewModel, but I would avoid that.

我要做的是将活动的ViewModel分解为较小的部分,并在我的Fragments中重新使用适当的ViewModel,这样我就不会有 God ViewModel ,在不同的 ViewModel 中也没有大致相同的代码.

What I would do is break the activity's ViewModel into smaller parts and reuse the proper ViewModel's in my Fragments, so that I wouldn't have a God ViewModel, nor roughly the same code in different ViewModel's.

这篇关于片段的ViewModel而不是访问Activity ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆