介绍
butterknife也是一个依赖注入框架,借助annonation实现view的快速初始化,解除findViewById的烦恼
工程引入
配置project的build.gradle,来引入android-apt插件1
2
3
4
5
6
7
8buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}
配置module的build.gradle,apply the android-apt plugin,and add the Butter Knife dependencies.
| 1 | 
 | 
注意
butter knife尽量不要在library中使用,如果要用,还得转R2,太麻烦了
使用
使用@BindView注解view
| 1 | class ExampleActivity extends Activity { | 
Resource的注解
@BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString
| 1 | class ExampleActivity extends Activity { | 
non-activity的注解
| 1 | public class FancyFragment extends Fragment { | 
也可以在ViewHolder中使用
View List
| 1 | @BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name }) | 
使用apply方法,能一次操作整个View的List1
2ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);
包括设置属性1
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
Listener的绑定
| 1 | @OnClick(R.id.submit) | 
绑定的释放
由于Fragment的生命周期区别于activity,当我们在Fragment的onCreatView做绑定的时候,要在onDestoryView中设置views为null。Butter Knife 返回一个Unbider实例来解决这个问题,
eg:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class FancyFragment extends Fragment {
  @BindView(R.id.button1) Button button1;
  @BindView(R.id.button2) Button button2;
  private Unbinder unbinder;
  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    unbinder = ButterKnife.bind(this, view);
    // TODO Use fields...
    return view;
  }
  @Override public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }
}
更懒的方式
使用Zelezny插件,一键生成view
如何找到插件