您的位置:首页 > Web前端

ButterKnife 8.0.1

2016-06-23 10:41 363 查看
自己在开发新的项目时,比较喜欢使用新版本的开发框架。今天在引入Butter Knife依赖时,发现它版本升级到了8.0.1。果断下载之。然而在使用中出现了一些问题:

@Bind()
标签没有了
运行项目时View会提示NullPointerException空指针的问题

此次Bug的出现,结束了一个
@Bind
标签走天下的局面。以前只是粗略的阅读过官方文档,这次打算深入的阅读一下看看会不会有新的惊喜。

Butter Knife 简单介绍

Annotate fields with @Bind and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.

这个就不过多解释啦,Butter Knife的使用即使相当于我们不用它的时候写的findViewById()。原来我们的代码是下面这样

TextView textView = (TextView) findViewById(R.id.hello_world);

用过之后,我们的代码是这样的:

@BindView(R.id.hello_world) TextView textView;
}

看着好像没有省下多少的代码量,但是一个页面中控件的数量多起来之后,它的优势就显示出来了。用过的都说好!

Butter Knife的更多用法

在Activity中的使用

大V说的好,talk is cheap, show me the code. 先给出一个比较全的例子:

class ExampleActivity extends Activity {
@BindView(R.id.title) TextView title;
@BindView(R.id.button1) Button button;
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.graphic) Drawable graphic;
@BindColor(R.color.red) int red; // int or ColorStateList field
@BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// TODO Use fields...
}
}

首先我们要注意的是
onCreate()
函数中 ButterKnife.bind(this),它调用了
bind()
方法是为了生成代码执行查找对应的View,即在生成
findViewById()
代码。忘了Butter Knife是在哪一个版本之后,就变成了编译时执行,而非运行时执行。所以我们就放心大胆的用吧,并不会产生运行效率问题。

Butter Knife将原来的
@Bind
注解,修改为
@BindXxx
。这样我们就可以使用这把黄油小刀做更多的事情。可以用
@BindBool
@BindColor
@BindDimen
@BindDrawable
@BindInt
@BindString
通过绑定R.bool以及其他对应id来进行资源的预定义。

在非Activity视图中使用

随着我们的项目越来越丰富,我们就会提出疑问,我们在Fragment或者Adapter中该怎么绑定视图呢?Butter Knife已经为我们考虑到了这个问题,只要你能提供根视图(view root),它就可以在任意对象中执行绑定操作。

public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(R.id.button2) Button button2;

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
ButterKnife.bind(this, view);
// TODO Use fields...
return view;
}
}

还可以帮助你在为一个listview做适配器(adapter)时简化ViewHolder的创建:

public class MyAdapter extends BaseAdapter {
@Override public View getView(int position, View view, ViewGroup parent) {
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 view;
}

static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;

public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}

这个时候我们就可以总结出规律了吧:可以在任何允许调用 findViewById 的地方调用 ButterKnife.bind

其他提供的Apis:

你可以绑定根视图是activity的任意对象,如果你在使用MVC模式进行开发,你可以通过ButterKnife.bind(this, activity)绑定该控制器(controller)。
使用ButterKnife.bind(this)绑定一个view中的子view中的字段,如果你在一个布局文件中使用<merge>标签,并且该布局在一个自定义控件的结构体中inflate进入,那你便可以在后面使用它来进行绑定其中字段。另外,从XML中inflate的自定义视图类型可以在onFinishInflate()回调方法中使用它。

使用Butter Knife中
@OnClick
注解绑定监听器

只需要像下面一样的写法就好了,又是一个少些代码的利器

@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();
}
}

Fragment中解除绑定

Fragment和Activity相比有着不一样的生命周期,在onCreateView中绑定一个fragment,需要在onDestroyView中将它的所有view都设为null。butter knife会在你bind的时候返回一个Unbinder类型的实例给你,你可以在适当的生命周期中调用该实例对象的unbind方法进行解绑。

public 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();
}
}

将Butter Knife添加到自己的项目中

compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'

在之前,我们只需要添加了butterknife的依赖就可以了,现在需要多做一件事情:

更改
build.gradle(Module:app)


buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1'
}


如果你觉得Butter Knife 的效率还是不够快,我推荐一个Android Studio上的一个插件:
ButterKnife Zelezny

用上它,一键生成view 注入不是梦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息