使用匕首2查看依赖注入 [英] View dependency injection with dagger 2

查看:173
本文介绍了使用匕首2查看依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义视图,扩展了 TextView 。我应该在哪里调用我的组件来注入视图?

I have a custom view extending a TextView. Where should I call my component to inject the view?

component.inject(customTextView);


推荐答案

所以,我发现我需要在我的自定义视图的构造函数中添加注入(在所有这些中,或者调用另一个)

So, I've find out that I need to add the injection in the constructor of my custom view (in all of them, or make one call the other)

示例:

public class CustomTextView extends TextView {
   @Inject
   AnyProvider anyProvider;

   public CustomTextView(Context context) { this(context, null); }
   public CustomTextView(Context AttributeSet attrs) { 
      super(context, attrs);
      Application.getComponent(context).inject(this);
   }
}

这篇关于使用匕首2查看依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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