奶油视图注射 [英] Butterknife View injection

查看:177
本文介绍了奶油视图注射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现一个非常有趣的依赖注入库,名为 ButterKnife 。使用 ButterKnife 很容易将视图注入活动或片段。

  class ExampleActivity extends Activity {
@InjectView(R.id.title)TextView title;
@InjectView(R.id.subtitle)TextView subtitle;
@InjectView(R.id.footer)TextView footer;

@Override public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.inject(this);
// TODO使用注入视图...
}
}

但是,如果使用依赖注入,那些视图必须是 public ,以便 Butterknife 可以注入它(使用 private 字段导致异常字段不能是私有或静态)。



在我以前的项目中,我总是把所有的成员字段(包括视图) private ,因为我认为这是最好的做法(信息隐藏等)现在我想知道是否有一个理由,不应该让所有的观点 public ?在这种情况下,我不能使用 ButterKnife ,但我想使用它,因为它简化了代码很多。

解决方案

首先,黄油刀不是依赖注入库。您可以将其视为一个样板修复库,因为它所做的只是替换 findViewById 和各种 setXxxListener 调用。 p>

Butter Knife要求视图不是私有的原因是实际生成设置字段的代码。它生成的代码与您的类位于同一个包中,这就是为什么字段必须是私有的,受保护的或公开的。如果该字段是私有的,则生成的代码将无法编译,因为它无法访问私有字段。



生成的代码如下所示:

  public static void inject(ExampleActivity target,ExampleActivity source){
target.title =(TextView)source.findViewById(R.id.title) ;
target.subtitle =(TextView)source.findViewById(R.id.subtitle);
target.footer =(TextView)source.findViewById(R.id.footer);
}

当您调用 ButterKnife.inject(this)它查找这个生成类,并调用注入方法与您的实例 ExampleActivity 两者字段的目的地和 findViewById 调用的来源。


I stumbled across a very interesting Dependency Injection library called ButterKnife. Using ButterKnife it's easily possible to inject Views into activities or fragments.

class ExampleActivity extends Activity {
  @InjectView(R.id.title) TextView title;
  @InjectView(R.id.subtitle) TextView subtitle;
  @InjectView(R.id.footer) TextView footer;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.inject(this);
    // TODO Use "injected" views...
  }
}

However if using Dependency Injection those Views must be public so that Butterknife can inject it (using private fields results in an exception fields must not be private or static).

In my past project I always made all the member fields (including the views) private as I thought this is best practice (information hiding etc.) Now I am wondering if there is a reason why one should not make all the views public? In this case I cannot use ButterKnife but I want to use it because it simplifies the code a lot.

解决方案

First off, Butter Knife is not a dependency injection library. You can think of it as a boilerplate reduction library since all it does is replace findViewById and various setXxxListener calls.

The reason that Butter Knife requires views not be private is that is actually generates code which sets the fields. The code that it generates lives in the same package as your class which is why the field must be package-private, protected, or public. If the field was private the generated code would fail to compile since it cannot access the private field.

The generated code looks something like this:

public static void inject(ExampleActivity target, ExampleActivity source) {
  target.title = (TextView) source.findViewById(R.id.title);
  target.subtitle = (TextView) source.findViewById(R.id.subtitle);
  target.footer = (TextView) source.findViewById(R.id.footer);
}

When you call ButterKnife.inject(this) it looks up this generate class and calls the inject method with your instance of ExampleActivity as both the destination for the fields and the source for findViewById calls.

这篇关于奶油视图注射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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