一.ButterKnife是什么?
由Square公司Jake Wharton大神开源了一个神奇的框架叫做ButterKnife,这个框架虽然也采用了注解进行注入,不过人家可是编译期生成代码的方式,对运行时没有任何副作用,见效快,只是编译期用一点点时间,其功能有
1.@BindView来替换调用findViewById()
2.使用@OnClick注解来消除由监听器带来的匿名内部类
3.通过对resource进行注解来消除多余的资源查找代码
4.多个View放在一个数组或者list中。然后一次性可以同时对这些View的动作,属性等进行操作
ButterKnife官网:http://jakewharton.github.io/butterknife/
二.BUtterKnife在Android studio中引用
在app下(局部)的build.gradle引用jar
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
如:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
//注解
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
在开头apply plugin: 'com.android.application'后面加上
apply plugin: 'com.neenbedankt.android-apt'
如:
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
在项目下(外部)的build.gradle的dependencies 里面加上
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
如:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
三.ButterKnife常见的使用情况
1.acticity中组件使用
在setContentView(layout_id)后面引入
ButterKnife.bind(this);
在控件上使用@BindView(R.id.xx)进行绑定
如:
@BindView(R.id.login_progress)
View mProgressView;
@BindView(R.id.login_form)
View mLoginFormView;
2.fragment中组件使用
3.事件使用
4.viewholder中使用
2.3.4使用方法看官网说明
四.Android ButterKnife Zelezny插件使用
AS - File - plugins搜索框中输入ButterKnife,如果没有安装则搜索不到,没有安装点击
按钮,在Browse Repositories界面的搜索框中输入butterknife如下,点击Android ButterKnife Zekezny。点击右侧安装按钮安装,安装完成后重启即可。