简体   繁体   English

了解Android架构组件示例GithubBrowserSample:ViewModelModule,ViewModel参数

[英]Understanding Android Architecture Components example GithubBrowserSample: ViewModelModule, ViewModel parameters

One of the most up to date samples covering Android Architecture Components is GithubBrowserSample provided by Google. 涵盖Android架构组件的最新样本之一是Google提供的GithubBrowserSample I reviewed the code and a few questions arose: 我查看了代码并出现了一些问题:

  1. I have noticed that ViewModelModule is included in AppModule . 我注意到ViewModelModule包含在AppModule中 It means that all the viewmodels are added to the DI graph. 这意味着所有视图模型都被添加到DI图中。 Why that is done in that way instead of having separate Module for each Activity/Fragment that would provide only needed ViewModel for specific Activity/Fragment? 为什么以这种方式完成,而不是为每个Activity / Fragment提供单独的Module ,只为特定的Activity / Fragment提供所需的ViewModel

  2. In this specific example, where viewmodels are instantiated using GithubViewModelFactory is there any way to pass a parameter to the specific ViewModel ? 在这个具体的例子中,使用GithubViewModelFactory实例化视图模型有没有办法将参数传递给特定的ViewModel Or the better solution would be to create a setter in ViewModel and set needed param via setter? 或者更好的解决方案是在ViewModel创建一个setter并通过setter设置所需的param?

  1. [...] It means that all the viewmodels are added to the DI graph. [...]这意味着所有视图模型都被添加到DI图中。 Why that is done in that way instead of having separate Module for each Activity/Fragment [...]? 为什么以这种方式完成而不是为每个Activity / Fragment [...]分别设置模块?

They are added to the DI graph, but they are not yet created. 它们被添加到DI图中,但尚未创建。 Instead they end up in a map of providers, as seen in the ViewModelFacory . 相反,它们最终出现在提供者的地图中,如ViewModelFacory中所示

@Inject
public GithubViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) { }

So we now have a GithubViewModelFactory that has a list of providers and can create any ViewModel that was bound. 所以我们现在有一个GithubViewModelFactory ,它有一个提供者列表,可以创建任何绑定的ViewModel Fragments and Activities can now just inject the factory and retrieve their ViewModel. 片段和活动现在可以注入工厂并检索他们的ViewModel。

@Inject
ViewModelProvider.Factory viewModelFactory;

// ...later...
repoViewModel = ViewModelProviders.of(this, viewModelFactory).get(RepoViewModel.class);

As to the why...alternatively you could create a ViewModelProvider.Factory for every Activity / Fragment and register the implementation in every Module. 至于为什么......或者你可以为每个Activity / Fragment创建一个ViewModelProvider.Factory ,并在每个Module中注册实现。 This would be a lot of duplicated boilerplate code, though. 但是,这将是许多重复的样板代码。

  1. In this specific example, where viewmodels are instantiated using GithubViewModelFactory is there any way to pass a parameter to the specific ViewModel? 在这个具体的例子中,使用GithubViewModelFactory实例化视图模型有没有办法将参数传递给特定的ViewModel? Or the better solution would be to create a setter in ViewModel and set needed param via setter? 或者更好的解决方案是在ViewModel中创建一个setter并通过setter设置所需的param?

It seems like all the ViewModels only depend on @Singleton objects—which is necessary, since they all get provided from the AppComponent. 似乎所有ViewModel都只依赖于@Singleton对象 - 这是必要的,因为它们都是从AppComponent提供的。 This means that there is no way to pass in "parameters" other than other @Singleton dependencies. 这意味着除了其他@Singleton依赖项之外,无法传入“参数”。

So, as you suggested, you'd either have to move the factory down into the Activity / Fragment component so that you can provide lower-scoped dependencies, or use a setter method. 因此,正如您所建议的那样,您必须将工厂向下移动到Activity / Fragment组件中,以便您可以提供较低范围的依赖项,或使用setter方法。

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

相关问题 Android架构组件GithubBrowserSample单元测试理解 - Android Architecture Components GithubBrowserSample unit test understanding Android MVVM + 数据绑定 + 架构组件(ViewModel 和 Room)示例 - Example of Android MVVM + Databinding + Architecture Components (ViewModel and Room) Android架构组件ViewModel上下文 - Android Architecture Components ViewModel Context Android架构组件:绑定到ViewModel - Android Architecture Components: bind to ViewModel Android架构组件ViewModel上下文问题 - Android Architecture Components ViewModel context issue Android架构组件:使用ViewModel for RecyclerView项目 - Android Architecture Components: using ViewModel for RecyclerView items Android架构组件ViewModel - 与Service / IntentService的通信 - Android Architecture Components ViewModel - communication with Service/IntentService Android架构组件 - ViewModel Observable和Proguard - Android Architecture Components - ViewModel Observable & Proguard ViewModel 实例 - Activity UI - Android 架构组件 - ViewModel instance - Activity UI - Android Architecture Components Android体系结构组件室ViewModel CompleteableFormAction - Android Architecture Components Room ViewModel CompleteableFormAction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM