butterknife官方地址
项目集成
在app module的build.gradle文件下添加如下依赖
android {
...
// Butterknife requires Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
//kapt 'com.jakewharton:butterknife-compiler:10.2.1' //Kotlin
}
插件导入
AndroidStudio-->setting-->pluign: 搜索并安装
android-butterknife-zelezny
自定义全局变量前缀
插件安装后,在AS-->setting-->Other Settings-->ButterknifeZelezny中,有2个输入框,可修改前缀和viewHoder的名称
使用
1. activity中的使用
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(bindLayoutID());
mBind = ButterKnife.bind(this);
initView(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mBind != null && mBind != Unbinder.EMPTY) {
mBind.unbind();
mBind = null;
}
}
@Override
protected int bindLayoutID() {
return R.layout.activity_main;
}
鼠标停留在R.layout.activity_main然后按快捷键alt+insert选择生成butterknife injections, 完成控件的绑定.
2. 绑定资源
@BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString等注解可以绑定资源作为全局变量使用
参考 ButterKnife支持的所有注解
@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
3.非Activity中使用
3.1 在Fragment中使用
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();
}
}
3.2在adapter中使用
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);
}
}
}
4 绑定多组件
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List nameViews;
5 绑定控件的点击事件
@OnClick(R.id.button)
public void onClick(Button button) {
ToastUtils.showShort(R.string.click);
button.setText(R.string.click);
}
//无参数点击
@OnClick(R.id.button)
public void onClick( ) {
ToastUtils.showShort(R.string.click);
}
//参数为View
@OnClick(R.id.button)
public void onClick(View view ) {
ToastUtils.showShort(R.string.click);
}
点击函数里面的参数可以是具体的某个控件,也可以不带参数, 另外ButterKnife的点击事件是防抖动的,可以有效避免短时间内多次点击.有兴趣的可以参看底部源码解析篇
6 空绑定
//@Nullable用于全局变量
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
//@Optional 用于方法
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// TODO ...
}
我的ButterKnife相关文章
ButterKnife(一): 使用篇
ButterKnife(二): 原理解析篇
ButterKnife(三): ButterKnifeProcessor解析