首先奉上ButterKnife的“图标”。
官方解释,很好懂的就不翻译了。
Field and method binding for Android views which uses annotation processing to generate boilerplate code for you.
- Eliminate findViewById calls by using @BindView on fields.
- Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
- Eliminate anonymous inner-classes for listeners by annotating methods with @OnClick and others.
- Eliminate resource lookups by using resource annotations on fields.
本文基于ButterKnife8.4.0写的,如果和其他版本有冲突也是正常的
在项目中添加依赖
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
使用的时候应该在activity的onCreate方法中执行ButterKnife.bind(this); 或者在Fragment的onCreateView中执行ButterKnife.bind(this, view);如果在Adapter中使用稍后介绍。
好了,配置完成后就开始介绍使用方法了:
- Bind View
@BindView(R.id.tv)
TextView mTv;
@BindView(R.id.btn)
Button mBtn;
- Bind Resource
@BindString(R.String.name)
String mName;
@BindDrawable(R.Drawable.icon)
Drawable mIcon;
@BindColor(R.color.red)
int mRed;
@BindDimen(R.dimen.space)
Float mSpace;
- Bind 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");
// 其他操作
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);
}
}
}
- Bind Views
@BindViews({ R.id.name1, R.id.name2, R.id.name3 })
List<EditText> mNameViews;
// 可以对其进行其他设置,比如:
ButterKnife.apply(mNameViews, DISABLE);
ButterKnife.apply(mNameViews, View.ALPHA, 0.0f);
// 或者
@OnClick({R.id.tv,R.id.tv2})
public void viewClick(TextView tv){ // 如果不需要区分是哪个控件被点击了,里边的参数可以不要
switch (tv.getId()){
case R.id.tv:
mTextView.setText(++i+"");
break;
case R.id.tv2:
mTextView2.setText(++i+"");
break;
}
}
-
Bind ListView
这个名字起的不少,应该说是绑定容器类list吧
@OnItemSelected(R.id.mListView)
void onItemSelected(int position) {
// 其他操作
}
- Bind OnLongClick
@OnLongClick( R.id.mButton )
public boolean showToast2(){ // 里边可以写上Button btn,如果需要的话
Toast.makeText(this, "long click", Toast.LENGTH_SHORT).show();
return true ;
}
- Bind OnTextChanged
@OnTextChanged(value = R.id.mEditText, callback = OnTextChanged.Callback.BEFORE_TEXT_CHANGED)
void beforeTextChanged(CharSequence s, int start, int count, int after) {
// 操作
}
@OnTextChanged(value = R.id.mEditText, callback = OnTextChanged.Callback.TEXT_CHANGED)
void onTextChanged(CharSequence s, int start, int before, int count) {
// 操作
}
@OnTextChanged(value = R.id.mEditText, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void afterTextChanged(Editable s) {
// 操作
}
- Optional Bind
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// 操作
}
-
解除绑定
在Activity的onDestroy中unregister,如果是Fragment则是在onDestroyView中unbind。
// 这个是Activity的
@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
// 下边是Fragment的
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
- 关于混淆
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
-
待续
上边只是一些平时用到的总结,后期用到其他的方法再继续添加,看官们如果用到了上述没有的方法或值得记录的可以留言告诉我,欢迎交流。
最后奉上ButterKnife的github地址: https://github.com/JakeWharton/butterknife