目录
·前言
·简介
·ButterKnife的优势
·使用须知
·使用步骤(流程)
前言
因为在日常的Android开发工作中我们会经常面对一些非常机械化且重复的代码,像是findViewById或setXXXListener等操作。接下来我要说的是一个功能很强大的Android开发框架ButterKnife(黄油刀),该框架的作者是JakeWharton。ButterKnife是一个注解型的View注入框架,在开发中使用此框架既可以增加开发效率,也增强了代码的可读性。
简介
ButterKnife框架的作用就是对一个成员变量使用@BindView注解,并传入一个View ID,ButterKnife就能够帮你找到布局中对应的View,并自动的进行转换(将View转换为特定的子类)。
与缓慢的反射相比,ButterKnife使用再编译时生成的代码来执行View的查找,因此不必担心注解的性能问题。
class ButterKnifeActivity extends Activity{
@BindView(R.id.head) TextView mHeader;
@BindView(R.id.body) TextView mBody;
@BindView(R.id.foot) TextView mFooter;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.butter_activity);
ButterKnife.bind(this);
}
}
ButterKnife的优势
1.减少没营养且繁琐的代码,增加开发效率
2.代码更加清晰,可读性强(对于不使用该框架的开发者学习过后也会有相同感受)
3.配置使用起来很简便,并且不影响App的运行效率
使用须知
1.属性布局不能用private 或者 static 修饰,否则会报错。
2.Activity ButterKnife.bind(this);必须在setContentView();之后。当父类bind绑定过后,继承父类的子类便不需要再次bind
3.Fragment ButterKnife.bind(this,mRootView);
4.不能通过注解实现setContentView()。
使用步骤(流程)
一、导入ButterKnife jar包
1.AS用户可以直接在工具中通过File->Project Structure->Dependencies->Library dependency搜索butterknife添加导入即可。
2.可以通过maven和gradle配置,如果在Library项目中使用要按git上的操作步骤进行配置,否则无法找到view。
ButterKnife git地址:https://github.com/JakeWharton/butterknife
二、常见使用方法
1.绑定Activity
ButterKnife.bind(this);
由于每次都要在Activity中的onCreate绑定Activity,所以为了增加开发效率可以提前写一个BaseActivity(ButterKnife.bind(this))完成绑定,然后需要用到ButterKnife的子类去继承BaseActivity。
2.绑定Fragment
ButterKnife.bind(this,mRootView);//绑定framgent
ButterKnife.unbind(this,view);
3.绑定View
@BindView(R.id.user) EditText username;
@BindView(R.id.pass) EditText password;
4.绑定资源
@BindColor(R.color.red) int textColor;//颜色
@BindDrawable(R.mipmap.ic_launcher) Drawable drawable; //图片
@Bind(R.id.imageView) ImageView mImageView;//图片控件
5.监听器的绑定:使用ButterKnife监听器能够自动的绑定到特定的执行方法上
a.直接绑定一个方法
@OnClick(R.id.submit)
public void submit(View view) {
}
b.所有监听方法的参数是可选的
@OnClick(R.id.submit)
public void submit() {
}
c.指定一个特定的类型,ButterKnife也能识别
@OnClick(R.id.submit)
public void goodMoring(Button button) {
button.setText("Good Morning!");
}
d.可以指定多个View ID到一个方法上,这样这个方法就成为了这些View的共同事件处理。
@OnClick({ R.id.door1, R.id.door2, R.id.door3 })
public void pickDoor(DoorView door) {
if (door.hasPrizeBehind()) {
Toast.makeText(this, "My Boy!", LENGTH_SHORT).show();
} else {
Toast.makeText(this, "My Girl!", LENGTH_SHORT).show();
}
}
e.自定义View时,绑定事件监听不需要指定ID
public class MyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}
三、重置绑定
Fragment的生命周期与Activity不同。在Fragment中如果你在onCreateView中使用绑定,那么你需要在onDestroyView中设置所有view为null。为此,ButterKnife返回一个Unbinder实例以便于你进行这项处理。在合适的生命周期回调中调用unbind函数就可完成重置。
public class ButterKnifeFragment 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();
}
}
四、可选绑定
在默认情况下,@bind和监听器的绑定是必须的,如果目标view没有找到的话,ButterKnife将会抛出个异常。
如果你并不想使用这样的默认行为而是想创建一个可选的绑定,那么你只需要在变量上使用@Nullable注解或在函数上使用@Option注解。
注意:任何名为@Nullable的注解都可以使用在变量上。但还是强烈讲义使用Android注解库中的@Nullable。
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// TODO ...
}
五、对于包含多个方法的监听
当一个监听器包含多个回调函数时,使用函数的注解能够对其中任何一个函数进行绑定。每一个注解都会绑定到一个默认的回调。你也可以使用callback参数来指定一个其它函数作为回调。
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO ...
}