Dagger Hilt - 如何将 ViewModel 注入适配器? [英] Dagger Hilt - How do I inject the ViewModel into the Adapter?

查看:63
本文介绍了Dagger Hilt - 如何将 ViewModel 注入适配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ViewModel 注入适配器.它在注入 Fragment 时工作正常.

I am trying to inject the ViewModel into the adapter. It works fine while injecting into Fragment.

视图模型:

class HomeViewModel @ViewModelInject constructor(
): ViewModel() 

片段:

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>(
R.layout.fragment_home
) {

private val viewModel: HomeViewModel by viewModels()

目前没有问题.但是当我尝试注入适配器时出现问题.

There is no problem so far. But problems arise when I try to inject into the adapter.

class HomeListAdapter @Inject constructor(
): BaseListAdapter<Users>(
itemsSame = { old, new -> old.username == new.username },
contentsSame = { old, new -> old == new }
) {



private val viewModel: HomeViewModel by viewModels() //viewModels() unresolved reference

更新:

如果我尝试使用构造函数注入或字段注入,我会收到以下错误:

If I try to use constructor injection or field injection I get the following error:

  error: [Dagger/MissingBinding] ***.home.HomeViewModel cannot be       provided without an @Inject constructor or an @Provides-annotated method.
  public abstract static class ApplicationC implements App_GeneratedInjector,
                         ^
      ***.home.HomeViewModel is injected at
          ***.home.adapter.HomeListAdapter.viewModel
      ***.home.adapter.HomeListAdapter is injected at
          ***.home.HomeFragment.viewAdapter
      ***.home.HomeFragment is injected at
                     ***.home.HomeFragment_GeneratedInjector.injectHomeFragment(***.home.HomeFragment) [***.App_HiltComponents.ApplicationC →    ***.App_HiltComponents.ActivityRetainedC → ***.App_HiltComponents.ActivityC → ***.App_HiltComponents.FragmentC]

适配器:

class HomeListAdapter @Inject constructor(
): BaseListAdapter<Users>(
itemsSame = { old, new -> old.username == new.username },
contentsSame = { old, new -> old == new }
) {


@Inject lateinit var viewModel: HomeViewModel;

推荐答案

一般来说,你不应该将 ViewModel 注入到 Adapter 中,因为 Adapter 是 presentation 层的一部分,是 Android 特有的东西.ViewModel 通常是独立于 Android 的东西,尽管来自 AAC 的 ViewModel 与它相关联.

Generally, you should not inject ViewModel into Adapter, because Adapter is a part of presentation layer and an Android-specific thing. ViewModel is something independent on Android in general, though ViewModel from AAC is tied to it.

您应该在 ViewModel 中获取数据并通过 LiveData 将其传递给 Fragment,然后从 Fragment 中填充您的 Adapter.

You should get your data in ViewModel and pass it to Fragment via LiveData, and then populate your Adapter from within Fragment.

by viewModels() 未在 Adapter 中定义,因为它是 Fragment 的扩展函数,只能在 Fragment 中使用.因此,将您的 ViewModel 从 Adapter 移回 Fragment.这也将修复您的编译错误,因为 Hilt 不会注入到适配器中.

by viewModels() is not defined in Adapter since it is an extension function of Fragment and could be used only within Fragment. So, move your ViewModel away from Adapter back to Fragment. That will also fix your compilation error since Hilt doesn't inject into Adapters.

这篇关于Dagger Hilt - 如何将 ViewModel 注入适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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