Butterknife快速入门应用
工具和版本
创建于2018-01-20
当前butterknife版本8.8.1'
androidstudio版本3.0
背景
Bind Android views and callbacks to fields and methods.
butterknife是一个非常强大的视图绑定库,大大简化代码,并且不会因为反射而影响APP性能。
使用步骤
1、配置gradle
dependencies {
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
如果使用Kotlin,把annotationProcessor替换成kapt
2、butterknife的绑定
2.1、绑定activity
class ExampleActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);//绑定activity,注意在setContentView之后
// TODO Use fields...
}
}
2.2、绑定fragment
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;
}
}
2.3、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);
}
}
}
其他的绑定APIs:
如果活动是视图的根节点,如果你使用MVC的设计模式,你可以在控制器中使用ButterKnife.bind(this, activity).来完成绑定
用ButterKnife.bind(this)来绑定子视图,如果你在布局中使用了merge标签在一个自定义的视图里你可以立刻使用,或者从xml中填充自定义视图,可以在onFinishInflate()回调中使用。
2.4、view绑定
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@BindView(R.id.footer) TextView footer;
2.5、绑定多个view
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;
2.6、使用apply方法,可以同时操作多个view
static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {
@Override public void apply(View view, int index) {
view.setEnabled(false);
}
};
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {
@Override public void set(View view, Boolean value, int index) {
view.setEnabled(value);
}
};
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);
属性也可以设置:
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
2.7、资源绑定
class ExampleActivity extends Activity {
@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
// ...
}
2.8、事件监听器的绑定
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
方法参数可选。
@OnClick(R.id.submit)
public void submit() {
// TODO submit data to server...
}
定义一个特定的类型,也能自动转化。
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}
多个view的通用点击事件。
@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();
}
}
自定义view的点击事件绑定不需要设置id
public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}
2.9、解绑
当使用ButterKnife 绑定fragment时,在Fragment的onDestroyView中需要解绑。
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();
}
}
2.10、@Nullable和@Optional
在使用@BindView和@OnClick时,如果目标view找不到会抛出异常,应对这种情况,我们可以使用@Nullable和@Optional
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void o nMaybeMissingClicked() {
// TODO ...
}
注意:推荐”support-annotations“ library提供的@Nullable注解。
2.11、多方法的listener(Multi-method Listeners)。方法注解可以用来绑定到这些方法中的任何一个,每一个注解有一个默认的callback,指定它绑定到什么方法上,也可以通过callback参数指定为一个特定的方法.
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO ...
}
2.12、ButterKnife.findById()可以用来获取Activity,Dialog或View中的任何View.ButterKnife自动完成了类型转换,所以获取出来以后不用进行显式强转,直接赋值给具体的View类型引用即可.
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);
3、混淆编译的时候需要在proguard文件中添加
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
网友评论