butterKnife最近又更新了,现在已经是8.4版本了,看到一些网上的教程还停留在以前的版本上,于是打算自己总结下。
新版本与以前比较大的区别就是引入了apt插件,在使用配置上有了不同,现在有很多开源库都引入了apt插件,有时间可以研究下。然后新版本在API的调用上也与之前有一点不同,新版本现在支持资源的绑定了,以前是不可以的
- 引入依赖
在project/build.gradle里面加入mavenCentral仓库和apt插件,像这样:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
在app/build.gradle里面引入butterKnife和apt插件:
apply plugin: 'android-apt'
android {
...
}
dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
这样就配置完成了
通过@BindView方式直接注入布局中的控件,例:
class ExampleActivity extends Activity {
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@BindView(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// TODO Use fields...
}
}
绑定各种资源:
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
// ...
}
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;
}
}
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);
}
}
}
绑定多个控件到一个集合里面:
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List nameViews;
设置监听,参数是可选的:
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
监听的参数类型自定义,然后它可以自动的强转:
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}
多个控件绑定在一个监听中:
@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();
}
}
自定义控件可以绑定监听而不指定id:
public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}
在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();
}
}
Listview的item监听:
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}