GitHub: https://github.com/JakeWharton/butterknife
正所谓懒才是程序猿的进步动力,为了精简代码,ButterKnife可以把我们从findViewById和setOnClickListener等一些繁琐的代码里面解放出来,可能一开始接受不了,因为代码可读性其实不高,不过看久了,还是很舒服的!
<pre>
module build gradle
ButterKnife的引入:
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'</pre>
<pre>
project build gradle
classpath 'com.android.tools.build:gradle:2.2.2'
</pre>
<h4>ButterKnife的作用</h4>
- 绑定控件ID
- (多控件支持)绑定监听,控件整合
- 绑定资源文件(Array,String,Boolean等)
ButterKnife绑定需要先指定绑定的View,之后从View中通过ID来获取控件对象,其也提供了findById方法
<h4>1.绑定资源文件</h4>
我们可以上Github上面来查看ButterKnife里面的源码,其中ButterAnnotate这个包包含ButterKnife里面所有的注释方式
这就是注解型绑定Resource拉,看起来是不是很好看?其实只是我排版好看而已而已
<h4>2.绑定控件ID</h4>
我们先分析一下,一个View里面包含几个控件,其中控件的ID必定不同,那么几个View中肯定有ID相同的,那么ButterKnife是通过View绑定控件的,那么我们从几个方面来写控件的绑定情况
2.1. 控件在Activity层上
<pre>public class MainActivity extends AppCompatActivity {
@Bind(R.id.title) TextView title;
@Bind(R.id.btn_knife_click) Button btnKnifeClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
btnKnifeClick.setText("btnKnifeClick");
ButterKnife.findById(this, R.id.btn_knife_click);
}
}</pre>
2.2. 控件在Fragment层上
<pre>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();
}
}</pre>
2.3. 控件在Adapter上
<pre>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);
}
}
}</pre>
<h4>3.绑定监听,控件整合</h4>
3.1. 监听判断
对于以上的监听,都可以通过看里面的源码来了解里面的参数组合,例如我用OnItemSelected来举例,里面有有两种枚举类型,NOTHING_SELECTED和ITEM_SELECTED
<pre>
@Target(METHOD)
@Retention(CLASS)
@ListenerClass(
targetType = "android.widget.AdapterView<?>",
setter = "setOnItemSelectedListener",
type = "android.widget.AdapterView.OnItemSelectedListener",
callbacks = OnItemSelected.Callback.class
)
public @interface OnItemSelected {
/** View IDs to which the method will be bound. */
int[] value() default { View.NO_ID };
/** Listener callback to which the method will be bound. */
Callback callback() default Callback.ITEM_SELECTED;
/** {@link OnItemSelectedListener} callback methods. /
enum Callback {
/*
* {@link OnItemSelectedListener#onItemSelected(android.widget.AdapterView, android.view.View,
* int, long)}
*/
@ListenerMethod(
name = "onItemSelected",
parameters = {
"android.widget.AdapterView<?>",
"android.view.View",
"int",
"long"
}
)
ITEM_SELECTED,
/** {@link OnItemSelectedListener#onNothingSelected(android.widget.AdapterView)} */
@ListenerMethod(
name = "onNothingSelected",
parameters = "android.widget.AdapterView<?>"
)
NOTHING_SELECTED
}
}
</pre>
而我们使用的时候,如果用户最终结果是有选择的,那么我们的代码就这样写:
<pre>
/**
*
* @param postion 点击的位置
* @param rowId 点击Item的Id
*/
@OnItemSelected(R.id.spinner)
public void onItemSelected(int postion,long rowId){
//参数是从源码枚举类型的parameters里面选择的
}
</pre>
如果用户最终没有选择结果,而我们又需要做判断
<pre>
@OnItemSelected(value = R.id.btn_knife_click,callback = OnItemSelected.Callback.NOTHING_SELECTED)
public void onItemSelected(){
//value 是控件ID,callback是源码枚举后面的类型
}
</pre>
3.2 监听整合
多个控件监听同一个事件
<pre> @OnClick({R.id.btn_knife_click,R.id.btn_knife_click,R.id.btn_knife_click})
public void onClick() {
System.out.println("btn_knife_onclick");
}</pre>
多个控件集成
<pre>
//多控件通过注释集合
@Bind({R.id.btn_knife_click, R.id.btn_knife_click, R.id.btn_knife_click})
List<Button> btns;
@Bind({R.id.btn_knife_click, R.id.btn_knife_click, R.id.btn_knife_click})
Button[] btns2;//绑定
public void apply() {
ButterKnife.apply(btns, actionAutoChangeName);
ButterKnife.apply(btns, actionChangeName, "name");
}
//批量修改列表控件内容
ButterKnife.Action<Button> actionAutoChangeName = new ButterKnife.Action<Button>() {
@Override
public void apply(Button view, int index) {
view.setText("name" + index);
}
};
//自定义修改控件内容
ButterKnife.Setter<Button, String> actionChangeName = new ButterKnife.Setter<Button, String>() {
@Override
public void set(Button view, String name, int index) {
view.setText(name + index);
}
};
</pre>
总的来说,我挺喜欢这个库的,很方便,使用很简单,熟悉了的话,真的可以省很多代码!