Butter Knife:一个安卓视图注入框架
Butter Knife:一个安卓视图注入框架
2014年5月8日 星期四
14:52
官网: http://jakewharton.github.io/butterknife/
GitHub地址: https://github.com/JakeWharton/butterknife
JavaDocs地址: http://jakewharton.github.io/butterknife/javadoc/
注:本随笔翻译自官网,做了一些整理和注释。来自我的OneNote笔记
大纲:
- @InjectView (Activity,Fragment)
- @InjectViews
- apply
- @OnClick
- reset
- @Optional
- @OnItemSelected
- TextView firstName = ButterKnife.findById(view, R.id.first_name);
Activity的注入方法:
//基本使用方法:在onCreate方法中调用ButterKnife.inject(this),然后就可以调用注解了 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... } } |
这个注入不是通过反射实现了(反射比较慢),而是直接生成代码。上面的三个注入最后生成如下的代码
public void inject(ExampleActivity activity) { activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578); activity.footer = (android.widget.TextView) activity.findViewById(2130968579); activity.title = (android.widget.TextView) activity.findViewById(2130968577); } |
Fragment的注入方法:
public class FancyFragment extends Fragment { @InjectView(R.id.button1) Button button1; @InjectView(R.id.button2) Button button2;
@Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); //仅仅是这里的区别 ButterKnife.inject(this, view); // TODO Use "injected" views... return view; } } |
简化ViewHolder的使用
public class MyAdapter extends BaseAdapter { @Override public View getView(int position, View view, ViewGroup parent) { //ViewHolder是一个普通的类,这个类包含了一个Adapter需要的所有View,然后设置到了tag中,方便复用 ViewHolder holder; if (view != null) { holder = (ViewHolder) view.getTag(); } else { view = inflater.inflate(R.layout.whatever, parent, false); holder = new ViewHolder(view); view.setTag(holder); }
holder.name.setText("John Doe"); // etc... return convertView; } //这里是ViewHolder:可以这样进行注入 static class ViewHolder { @InjectView(R.id.title) TextView name; @InjectView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) { ButterKnife.inject(this, view); } } } |
注入一个View列表:@InjectViews
//注入一个View列表 @InjectViews({ R.id.first_name, R.id.middle_name, R.id.last_name }) List<EditText> nameViews; //调用apply方法批量给View设置属性 ButterKnife.apply(nameViews, DISABLE); ButterKnife.apply(nameViews, ENABLED, false); //其中,DISABLE和ENABLED是两个接口的实现:Action,Setter static final Action<View> DISABLE = new Action<>() { @Override public void apply(View view, int index) { view.setEnabled(false); } } static final Setter<View, Boolean> ENABLED = new Setter<>() { @Override public void set(View view, Boolean value, int index) { view.setEnabled(value); } } ////View所有的属性都可以在apply方法中调用 ButterKnife.apply(nameViews, View.ALPHA, 0); |
点击监听器的注入:OnClickListener -> @OnClick
//简单使用 @OnClick(R.id.submit)ren public void submit() { // TODO submit data to server... } //你可以传入一个参数,Butter Knife会自动将注入的View转换为对应的类型 @OnClick(R.id.submit) public void sayHi(Button button) { button.setText("Hello!"); } //当然,也可以指定多个视图的IDs,用来进行通用的处理 @OnClick({ R.id.door1, R.id.door2, R.id.door3 }) public void pickDoor(DoorView door) { if (door.hasPrizeBehind()) { Toast.makeText(this, "You win!", LENGTH_SHORT).show(); } else { Toast.makeText(this, "Try again", LENGTH_SHORT).show(); } } |
注入的重置:reset
Fragment中,我们需要在onDestroyView中设置这些View为null,但是只要调用reset方法,Butter Knife会自动执行这个步骤。
public class FancyFragment extends Fragment { @InjectView(R.id.button1) Button button1; @InjectView(R.id.button2) Button button2;
@Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.inject(this, view); // TODO Use "injected" views... return view; }
@Override void onDestroyView() { super.onDestroyView(); ButterKnife.reset(this); } } |
可选的注入 @Optional
默认情况下,@InjectView和@OnClick注入是必须了,所以,如果找不到目标View就会抛出异常。如果想抑制这种情况,可以通过@Optional注解:
@Optional @InjectView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() { // TODO ... } |
多方法的监听@OnItemSelected
有些监听注解响应的方法有多个回调函数,所以,我们可以通过指定回调参数来实现多个回调方法的绑定。
@OnItemSelected(R.id.list_view) void onItemSelected(int position) { // TODO ... }
@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED) void onNothingSelected() { // TODO ... } |
BONUS:其实就是一个方便的静态方法而已
一般我们实例化一个View的时候是通过findById(id),然后强制转换实现的,这样的代码看着不雅观。所以,ButterKnife有一个方法自动帮我们进行这样的转换(Context可以为View和Activity),如下面的例子:
View view = LayoutInflater.from(context).inflate(R.layout.thing, null); TextView firstName = ButterKnife.findById(view, R.id.first_name); TextView lastName = ButterKnife.findById(view, R.id.last_name); ImageView photo = ButterKnife.findById(view, R.id.photo); |
最新版本:5.0.1
集成方法
Maven:
<dependency> <groupId>com.jakewharton</groupId> <artifactId>butterknife</artifactId> <version>5.0.1</version> </dependency> |
Gradle:
- 添加依赖
compile 'com.jakewharton:butterknife:5.0.1' |
- 消除lint warning
lintOptions { disable 'InvalidPackage' } |
- Some configurations may also require additional exclusions.
packagingOptions { exclude 'META-INF/services/javax.annotation.processing.Processor' } |
Procuard配置:(这个配置是为了打包apk的时候不至于把你的那些貌似没有用过的程序片段给删掉了)
-dontwarn butterknife.internal.** -keep class **$$ViewInjector { *; } -keepnames class * { @butterknife.InjectView *;} |