在Android編程我們要使布局文件如TextView葵姥、Button遗座、ImageView與代碼關(guān)聯(lián)起來就要使用到FindViewById這個方法舞丛,要為他們設(shè)置監(jiān)聽器的話就要setOnClickListener撒踪。雖然用起來很簡單偷俭,但如果布局控件一旦多了起來就會感覺很重復(fù)麻煩不便以后的維護(hù)走贪,常常會使我們分不清楚哪個控件才是我們想使用的佛猛。
使用ButtonKnife的優(yōu)勢
- 強(qiáng)大的View綁定和Click事件處理功能,簡化代碼坠狡,提升開發(fā)效率
- 方便的處理Adapter里的ViewHolder綁定問題
- 運(yùn)行時不會影響APP效率继找,使用配置方便
- 代碼清晰,可讀性強(qiáng)
配置ButtonKnife
在app->build gradle中添加依賴
compile 'com.jakewharton:butterknife:(butterknife:7.0.1)'
annotationProcessor 'com.jakewharton:butterknife-compiler:(butterknife:7.0.1)'
點(diǎn)擊按鈕:Sync Projects with Gradle Files重新編譯即可擦秽。
常見使用ButtonKnife的方法:
綁定view
@Bind(R.id.tvTitle)
TextView mTvTitle;
@Bind(R.id.iv_avatar)
ImageView mIvAvatar;
@Bind(R.id.tv_author)
TextView mTvAuthor;
@Bind(R.id.tv_time)
TextView mTvTime;
@Bind(R.id.wv_content)
WebView mWvContent;
之后View對象就可以直接使用了.
需要注意的是View變量聲明的時候不能為private或者static.
綁定資源文件
@BindString(R.string.app_name)
String appName;
@BindColor(R.color.red)
int textColor;
@BindDrawable(R.mipmap.ic_launcher)
Drawable drawable;//drawble
@Bind(R.id.imageview)
ImageView mImageView;
@Bind(R.id.checkbox)
CheckBox mCheckBox;
@BindDrawable(R.drawable.selector_image)
Drawable selector;
@BindString(R.string.app_name)
String appName;
@BindDrawable(R.mipmap.ic_launcher)
Drawable drawable;
為View設(shè)置監(jiān)聽
@OnClick(R.id.iv_avatar)
public void submit() {
// TODO submit data to server...
}
或者
@OnClick(R.id.iv_avatar)
public void submit(View view) {
// TODO submit data to server...
}
還可以批量為多個控件添加為同一個響應(yīng)函數(shù):
@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();
}
}
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;
}
}
優(yōu)化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("helloworld");
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);
}
}
}
可以看到ViewHolder類加了一個帶參數(shù)View的構(gòu)造方法,用注解標(biāo)記每個字段,再也不需要在getView()方法里調(diào)用findViewById()方法了码荔。
控件的批量操作
ButterKnife中還有一個好玩的地方莫過于控件的批量操作,我們可以定義統(tǒng)一的行為感挥,來設(shè)置給某一組控件缩搅,比如我想給我某一組的TextView修改背景顏色,可以定義如下行為:
ButterKnife.Action<View> CHANGECOLOR = new ButterKnife.Action<View>() {
@Override
public void apply(@NonNull View view, int index) {
view.setBackgroundColor(Color.RED);
}
};
使用方式如下:
ButterKnife.apply(textViews, CHANGECOLOR);
其中textViews是一個TextView的集合触幼。
如果你想給TextView設(shè)置不同的背景顏色硼瓣,那么還可以這樣來定義:
ButterKnife.Setter<View,List<Integer>> CHANGECOLOR2 = new ButterKnife.Setter<View, List<Integer>>() {
@Override
public void set(@NonNull View view, List<Integer> value, int index) {
view.setBackgroundColor(value.get(index));
}
};
使用方式:
List<Integer> colors = new ArrayList<>();
colors.add(Color.RED);
colors.add(Color.GREEN);
colors.add(Color.BLUE);
ButterKnife.apply(textViews,CHANGECOLOR2,colors);
注意
- ButtKnife初始化ButterKnife.bind(this)必須在加載布局資源之后(在setContentView(),Inflate等方法下面),所以ButterKnife不能在Application中初始化。
- ButtKnife在Fragment和Adapter中使用時置谦,初始化代碼有所不同,多了個參數(shù)VIew對象.ButterKnife.bind(this, mRootView);
- 使用ButterKnife設(shè)置控件點(diǎn)擊事件,其方法權(quán)限必須在默認(rèn)權(quán)限以上方可(包括默認(rèn)權(quán)限)
參考文獻(xiàn):http://jakewharton.github.io/butterknife/