butterKnife最近又更新了纵散,現(xiàn)在已經(jīng)是8.4版本了,看到一些網(wǎng)上的教程還停留在以前的版本上,于是打算自己總結(jié)下顺呕。
新版本與以前比較大的區(qū)別就是引入了apt插件,在使用配置上有了不同括饶,現(xiàn)在有很多開(kāi)源庫(kù)都引入了apt插件株茶,有時(shí)間可以研究下。然后新版本在API的調(diào)用上也與之前有一點(diǎn)不同图焰,新版本現(xiàn)在支持資源的綁定了启盛,以前是不可以的
- 引入依賴
在project/build.gradle里面加入mavenCentral倉(cāng)庫(kù)和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'
}
這樣就配置完成了
通過(guò)@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);
}
}
}
綁定多個(gè)控件到一個(gè)集合里面:
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;
設(shè)置監(jiān)聽(tīng)僵闯,參數(shù)是可選的:
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
監(jiān)聽(tīng)的參數(shù)類(lèi)型自定義卧抗,然后它可以自動(dòng)的強(qiáng)轉(zhuǎn):
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}
多個(gè)控件綁定在一個(gè)監(jiān)聽(tīng)中:
@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();
}
}
自定義控件可以綁定監(jiān)聽(tīng)而不指定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監(jiān)聽(tīng):
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}