Android ButterKnife 使用方法总结
16lz
2021-01-23
ButterKnife Introduction(介绍)
ButterKnife 是一个专注于Android系统的View注入框架,可视化一键生成。
-
官网:http://jakewharton.github.io/butterknife/
-
GitHub:https://github.com/JakeWharton/butterknife/
英:
- Eliminate findViewById calls by using @BindView on fields.
- Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
- Eliminate anonymous inner-classes for listeners by annotating methods with @OnClick and others.
- Eliminate resource lookups by using resource annotations on fields.
译:
- 使用@BindView 来代替findViewById 完成View的引用。
- 将多个View组合成list或者array,使用actions,setters或者属性来同时操作它们。
- 使用@OnClick等注解字段来注解方法,从而来代替监听器中的匿名内部类。
- 使用@BindString等注解字段来注解字段,从而来代替Context.getString等获取资源的方式。
How to build (如何够贱):
Project build.gradle
添加:
dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'}
分支 module build.gradle
添加:(如果只有主项目可忽略)
apply plugin: 'com.jakewharton.butterknife'dependencies { annotationProcessor "com.jakewharton:butterknife-compiler:8.4.0"}
分支 lib_common build.gradle
添加:
apply plugin: 'com.jakewharton.butterknife'dependencies { compile "com.jakewharton:butterknife:8.4.0" annotationProcessor "com.jakewharton:butterknife-compiler:8.4.0"}
How to use it (如何使用):
activity
使用:
import butterknife.ButterKnife;import butterknife.Unbinder;public abstract class BaseActivity extends AppCompatActivity { // Unbinder private Unbinder unbind; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setView setContentView(R.layout.main); // ButterKnife unbind = ButterKnife.bind(this); } @Override protected void onDestroy() { super.onDestroy(); unbind.unbind(); }}
Fragment
使用:
import butterknife.ButterKnife;import butterknife.Unbinder;public abstract class BaseFragment extends Fragment { protected View rootView; private Unbinder unbind; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (rootView == null) { rootView = inflater.inflate(this.getLayoutResource(), container, false); } unbind = ButterKnife.bind(this, rootView); return rootView; } //获取布局文件 protected abstract int getLayoutResource(); @Override public void onDestroyView() { super.onDestroyView(); unbind.unbind(); }}
Adaper
使用:
import butterknife.ButterKnife;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.item, parent, false); holder = new ViewHolder(view); view.setTag(holder); } holder.name.setText("hello"); return view; } static class ViewHolder { @BindView(R.id.name) TextView name; public ViewHolder(View view) { ButterKnife.bind(view); } }}
Butterknife
常用的注解:
Butterknife支持Activity,Fragment,View,Dialog,ViewHolder类内部的View绑定// 绑定View,避免findViewById,也可以用在ViewHolder里,必须是public@BindView(R2.id.textview) TextView mTextView; // 绑定多个view,只能用List不能用ArrayList@BindViews({ R2.id.button1, R2.id.button2, R2.id.button3}) List
ButterKnife Zelezny插件(自动生成代码)
How to install (如何安装)
-
in Android Studio:
File
→Settings
→Plugins
→Browse repositories
→搜索Android ButterKnife Zelezny
→Install
→Restart Android Studio
(安装后重启生效) -
download ButterKnife:
File
→Settings
→Plugins
→Install plugin from disk
(安装后重启生效)
How to use it (如何使用):
zelezny_animated.gif
- Make sure you have latest Butterknife lib on your classpath
- Right click on usage of desired layout reference (e.g. R.layout.main in your Activity or Fragment), then
Generate
andGenerate ButterKnife Injections
- Pick injections you want, you also have an option to create ViewHolder for adapters.
- Click
Confirm
and enjoy injections in your code with no work!
ButterKnife 代码混淆:
-keep class butterknife.** { *; } -dontwarn butterknife.internal.** -keep class **$$ViewBinder { *; } -keepclasseswithmembernames class * { @butterknife.* ; } -keepclasseswithmembernames class * { @butterknife.* ; }
更多相关文章
- android 检测sqlite数据表和表中字段(列)是否存在 (转)
- [Android]ButterKnife-无尽之刃-绑定视图控件和事件的快速开发工
- Android support-annotations 注解使用详解
- android 数据双向绑定学习笔记
- 一个编译时注解的 RxBus 库 - Apollo
- 在 Android 使用 Support Annotations 注解优化代码
- Android -- 跨应用绑定service(AIDL)