Annotation註解APT(五):注入框架ButterKnife
引言
作為一個程式設計師,特別是一個app程式設計師,在寫程式碼的時候,很大部分時間都花在佈局的編寫及控制元件的初始化和事件監聽方法上了,這樣就沒有時間來處理程式碼邏輯和結構了,現在ButterKnife框架可以為你節省很多時間,真的很節省的.
Android Studio中使用ButterKnife
1.可以直接在build.gradle中配置
top build.gradle
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
app build.gradle
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
...
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
1.也可以通過File->Project Structure -> app -> Dependencides-> + 搜尋butterknife的最新版本.
然後就可以使用了:
package com.example.fishmov.daggerdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "fish---";
protected Unbinder unbinder;
@BindView (R.id.btn)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unbinder = ButterKnife.bind(this);
Log.i(TAG, "" + button);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
}
注意:一定要在setContentView(R.layout.activity_main)後進行ButterKnife.bind(this);否則將報異常,當然我們會在onDestroy中進行unbinder.unbind().
對於這種處理,我們在app中可以採用一個基類的做法,避免重複編碼,比如:
Activity
package com.example.fishmov.daggerdemo;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* Created by fishmov on 17-6-22.
*/
public abstract class BaseActivity extends AppCompatActivity {
protected abstract int getContentViewId();
protected abstract void initView();
protected Unbinder unbinder;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
unbinder = ButterKnife.bind(this);
initView();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != unbinder) {
unbinder.unbind();
}
}
}
Fragment
package com.example.fishmov.daggerdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* Created by fishmov on 17-6-22.
*/
public abstract class BaseFragment extends Fragment {
protected abstract int getContentViewId();
protected abstract void initView();
protected Unbinder unbinder;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(getContentViewId(), container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView();
}
@Override
public void onDestroyView() {
if (null != unbinder) {
unbinder.unbind();
}
super.onDestroyView();
}
}
ViewHolder
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);
}
}
}
由於butterknife的用法比較簡單,也很好理解,後面將不再以例項說明,只提供相關用法:
@BindView(R.id.title) TextView title;
@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