ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,可视化一键生成。
ButterKnife 优势:
1、强大的View绑定和Click事件处理功能,简化代码,提升开发效率
2、方便的处理Adapter里的ViewHolder绑定问题
3、运行时不会影响APP效率,使用配置方便
4、代码清晰,可读性强
如何在项目中使用ButterKnife呢
首先在gradle配置文件中引入ButterKnife库依赖,这里在两个gradle文件中都要配置,注意红色部分为新加的配置,分别如下:
Project的gradle中:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
maven { url("https://oss.sonatype.org/content/repositories/snapshots/") }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
applicationId "dj.newsapp"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.alibaba:fastjson:1.2.17'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.4.0'
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
apt 'com.jakewharton:butterknife-compiler:8.4.0'
然后在给view事件设置操作时会报空指针异常!在ButterKnife 7.0.1中绑定View是用Bind(R.id.XXX)的方法;在8之后是用BindView(R.id.XXX)
配置好了,下面是如何使用ButterKnife注解。
Activity中绑定
1.绑定View:
a.绑定一个控件 :
// 定义控件并绑定
@BindView(控件的id)
public Button button1 ;
// onCreate()方法中初始化ButterKnife:
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
b.绑定多个控件:
@BindViews({控件1的id,控件2的id})
({ R.id.button1 , R.id.button2 , R.id.button3 })
public List<Button> buttonList ;
Fragment中绑定
前面一样,oncreate()中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_butterknife, container, false);
//绑定fragment
ButterKnife.bind( this , view ) ;
button1.setText( "I am a button ");
return view ;
}
2 @BindString() :绑定string 字符串
@BindString( R.string.titleStr; ) //绑定string 字符串
String titleStr;
3 @BindArray() : 绑定string里面array数组
String [] citys ;
button1.setText( citys[0] );
4.@BindBitmap( ) : 绑定Bitmap 资源
@BindBitmap( R.mipmap.icon ) //绑定Bitmap 资源
public Bitmap wifi_bitmap ;
5.@BindColor( ) : 绑定一个颜色值
@BindColor( R.color.colorAccent )
int black ; //绑定一个颜色值
6.@OnClick( ) : 绑定控件点击事件
@OnLongClick( ) : 绑定控件长按事件
(R.id.button1 ) //给 button1 设置一个点击事件
@OnLongClick( R.id.button1 )
public boolean showToast(){
Toast.makeText(this, "is a long click", Toast.LENGTH_SHORT).show();
return true ;
}