引言
不得不提之前开发Android项目时很繁琐的事情,写了若干activity.xml的布局文件,每个layout中又有若干的控件view,因此需要写若干的findViewById()方法。真心难受~
(1)ButterKnife是什么?
在开发过程中,我们总是会写大量的findViewById和点击事件,像初始化view、设置view监听这样简单而重复的操作让人觉得特别麻烦,当然不会偷懒的程序员不是好程序员,自然也出现了相应的解决方案----依赖注入。而ButterKnife
则是依赖注入中相对简单易懂的很不错的开源框架,(其实ButterKnife也不算严格意义上的依赖注入,后面文章中会做分析)。但ButterKnife作为JakeWharton大神写的注解框架被广泛应用于android开发中,自然也有它的过人之处。下面对它的使用过程进行描述。
(2)ButterKnife 有哪些优势?
强大的View绑定和Click事件处理功能,简化代码,提升开发效率
方便的处理Adapter里的ViewHolder绑定问题
运行时不会影响APP效率,使用配置方便
代码清晰,可读性强
(3) ButterKnife 和其他依赖注入框架的区别在哪里?
3.1 依赖注入框架的区别和联系:
在Android中主要使用的依赖注入框架为Dagger、Butter Knife、RoboGuice、Android Annotations。这里提供链接: Android’s Options for Dependency Injection: Dagger, RoboGuice, and ButterKnife
对应的翻译文章:Android依赖注入:Dagger、RoboGuice和ButterKnife.
3.2 当然,这种典型的问题当然少不了StackOverflow上去看看
大概意思:
Buffer Knife的目的为了注入到view,所以能够在非activity里面注入,也能注入到inflate的views里面。Dagger能够注入到任何你想要的对象,只要其在module类中。或者它是构造器。但是缺少对方法和字段的注入支持。Buffer knife只是避免样板代码findViewById
,仅此而已,所以不能算是一个真正的注入。只是一个view的代言。
(4)ButterKnife使用中有哪些注意的点呢?
- Activity中ButterKnife.bind(this);必须在setContentView();之后,且父类bind绑定后,子类不需要再bind
- Fragment中ButterKnife.bind(this, mRootView);
- 属性布局不能用private or static 修饰,否则会报错
- setContentView()不能通过注解实现。
- ButterKnife已经更新到版本7.0.1了,以前的版本中叫做@InjectView了,而现在改用叫@BindView,更加贴合语义。
- 在Fragment生命周期中,onDestoryView也需要Butterknife.unbind(this)
- ButterKnife不能在你的library module中使用哦!!这是因为你的library中的R字段的id值不是final类型的,但是你自己的应用module中确是final类型的。针对这个问题,有人在Jack的github上issue过这个问题,他本人也做了回答,属性必须是一个常量
(5)我还想更懒怎么办?
这里说的是添加插件,让你写代码更快捷。
Zelezny插件的使用的使用能让你变得更懒(也代码更快)
5.1 怎么使用插件?
(6)推荐链接:
- ButterKnife github地址
- 详解Dagger2
- Android’s Options for Dependency Injection: Dagger, RoboGuice, and ButterKnife(这里是翻译文章链接Android依赖注入:Dagger、RoboGuice和ButterKnife)
- Dagger官网
- 关于在library中应用ButterKnife出现的问题
- 8 个最优秀的 Android Studio 插件
ButterKnife使用详解
具体更新的使用方法可参考官网
本文参考的是8.8.1 版本的ButterKnife
使用心得:
1.Activity ButterKnife.bind(this);必须在setContentView();之后,且父类bind绑定后,子类不需要再bind
2.Fragment ButterKnife.bind(this, mRootView);
3.属性布局不能用private or static 修饰,否则会报错
4.setContentView()不能通过注解实现。(其他的有些注解框架可以)
使用步骤:
一.导入ButterKnife jar包:
1)如果你是Eclipse
,可以去官网下载jar包
2)如果你是AndroidStudio
可以直接 File->Project Structure->Dependencies->Library dependency 搜索butterknife即可,第一个就是
3)当然也可以用maven和gradle配置
MAVEN
<dependency>
<groupId>com.jakewharton</groupId>
<artifactId>butterknife</artifactId>
<version>(insert latest version)</version>
</dependency>
GRADLE
compile 'com.jakewharton:butterknife:(insert latest version)' //如8.8.1
annotationProcessor 'com.jakewharton:butterknife-compiler:(insert latest version)'
Be sure to suppress this lint warning in your build.gradle.(关闭)
lintOptions {
disable 'InvalidPackage'
}
注意:如果在Library 项目中使用要按如下步骤(github中有具体描述)否则无法找到view:
二.常见使用方法:
1)由于每次都要在Activity中的onCreate绑定Activity,所以个人建议写一个BaseActivity完成绑定,子类继承即可
注:ButterKnife.bind(this);绑定Activity 必须在setContentView之后:
实现如下(FragmentActivity 实现一样):
public abstract class BaseActivity extends Activity {
public abstract int getContentViewId();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
ButterKnife.bind(this);
initAllMembersView(savedInstanceState);
}
protected abstract void initAllMembersView(Bundle savedInstanceState);
@Override
protected void onDestroy() {
super.onDestroy();
ButterKnife.unbind(this);//解除绑定,官方文档只对fragment做了解绑
}
}
2)绑定fragment
public abstract class BaseFragment extends Fragment {
public abstract int getContentViewId();
protected Context context;
protected View mRootView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRootView =inflater.inflate(getContentViewId(),container,false);
ButterKnife.bind(this,mRootView);//绑定framgent
this.context = getActivity();
initAllMembersView(savedInstanceState);
return mRootView;
}
protected abstract void initAllMembersView(Bundle savedInstanceState);
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);//解绑
}
}
3)绑定view
Annotate fields with @BindView and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.
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...
}
}
4)绑定资源
RESOURCE BINDING
Bind pre-defined resources with @BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString, which binds an R.bool ID (or your specified type) to its corresponding field.
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
// ...
}
5)Adapter ViewHolder 绑定
NON-ACTIVITY BINDING
You can also perform binding on arbitrary objects by supplying your own view root.
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;
}
}
Another use is simplifying the view holder pattern inside of a list 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);
}
}
}
6)点击事件的绑定:不用声明view,不用setOnClickLisener()就可以绑定点击事件
a.直接绑定一个方法
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
b.所有监听方法的参数是可选的
@OnClick(R.id.submit)
public void submit() {
// TODO submit data to server...
}
c.定义一个特定类型,它将自动被转换
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}
d.多个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();
}
}
e.自定义view可以绑定自己的监听,不指定id
public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}
f.给EditText加addTextChangedListener(即添加多回调方法的监听的使用方法),利用指定回调,实现想回调的方法即可,哪个注解不会用点进去看下源码上的注释就会用了
@OnTextChanged(value = R.id.mobileEditText, callback = OnTextChanged.Callback.BEFORE_TEXT_CHANGED)
void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@OnTextChanged(value = R.id.mobileEditText, callback = OnTextChanged.Callback.TEXT_CHANGED)
void onTextChanged(CharSequence s, int start, int before, int count) {
}
@OnTextChanged(value = R.id.mobileEditText, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void afterTextChanged(Editable s) {
}
7)对一组View进行统一操作
a.装入一个list
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;
b.设置统一处理
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);
}
};
c.统一操作处理,例如设置是否可点,属性等
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);
An Android
Property
can also be used with theapply
method.
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
8)可选绑定:默认情况下,“绑定”和“监听”都是必需的。如果不能找到目标视图,则将抛出异常。所以做空处理
By default, both
@Bind
and listener bindings are required. An exception will be thrown if the target view cannot be found.
To suppress this behavior and create an optional binding, add a
@Nullable
annotation to fields or the@Optional
annotation to methods.
Note: Any annotation named
@Nullable
can be used for fields. It is encouraged to use the@Nullable
annotation from Android's "support-annotations" library.
@Nullable
@BindView(R.id.might_not_be_there)
TextView mightNotBeThere;
@Optional
@OnClick(R.id.maybe_missing)
void onMaybeMissingClicked() {
// TODO ...
}
MULTI-METHOD LISTENERS
Method annotations whose corresponding listener has multiple callbacks can be used to bind to any one of them. Each annotation has a default callback that it binds to. Specify an alternate using the callback parameter.
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO ...
}
三、代码混淆
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
四、Zelezny插件的使用
在AndroidStudio->File->Settings->Plugins->搜索Zelezny下载添加就行 ,可以快速生成对应组件的实例对象,不用手动写。使用时,在要导入注解的Activity 或 Fragment 或 ViewHolder的layout资源代码上,右键——>Generate——Generate ButterKnife Injections,然后就出现如图的选择框。(此动态图来自官网)
有一篇博客写的也蛮清晰的,推荐一下
Android Butterknife 8.4.0 使用方法总结