1. 程式人生 > >Android之ButterKnife用法詳解

Android之ButterKnife用法詳解

轉自:http://blog.csdn.net/leavessilent/article/details/60872096

相信很多開發Android的小夥伴,都厭倦了findViewById(),都是基本重複的操作,所以我們可以使用依賴注入框架來偷懶。目前,用的較多的兩種大概是ButterKnife和dagger,英文譯為黃油刀和匕首,聽名字就很酷。

今天我們就來詳細介紹ButterKnife的用法

ButterKnife是Square公司的Android之神JakeWharton開源的一款依賴注入框架,一把好用的黃油刀.

## 新增依賴 * 首先在Module的build.gradle中
dependencies {
  compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' }
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

在build.gradle第一行加上

apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
  • 在工程的build.gradle中
  dependencies {
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

在Activiy中使用

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    mUnbinder = ButterKnife.bind(this);
    // TODO Use fields...
  }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

首先在onCreate方法中呼叫`ButterKnife.bind(this)` 會得到一個 `Unbinder` 物件,然後在 `onDestroy` 方法中呼叫`unbind()`方法解除繫結。

* 繫結View

@BindView(R.id.tv_title)
View mTitleTv;
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
  • 當然它還可以一次性繫結多個view
@BindViews({ R.id.image_first, R.id.image_second, R.id.image_third})
List<ImageView> mImageViews;
  • 1
  • 2
  • 1
  • 2
  • ButterKnife還提供了一些簡潔的操作view的方法
// apply方法會自動遍歷傳入的陣列和集合,將所有imageviw透明度設為50%
ButterKnife.apply(mImageViews, View.ALPHA, 0.5f);

  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 此外我們還可以自定義對view的操作:
// 程式碼無實際意義,只是為了演示。
ButterKnife.apply(mImageViews, new ButterKnife.Action<ImageView>() {
         @Override
         public void apply(@NonNull ImageView view, int index) {
                view.setVisibility(View.GONE);
         }
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在這裡index是每個ImageView的下標,或者叫索引。

  • 繫結資源
@BindString(R.string.title)
String title;

@BindDrawable(R.drawable.graphic)
Drawable graphic

@BindColor(R.color.red)
int redColor;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

記住這些成員變數前面都不能加private修飾符。

  • 繫結監聽事件

單個view的監聽

 @OnClick(R.id.btn_back})
        public void onClick(View view) {
           // do sth
 }
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

多個View的監聽

    @OnClick({R.id.tv_finish, R.id.btn_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_finish:
                // do sth
                break;
            case R.id.btn_back:
                // do sth
                break;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
其他如onFocusChange、OnItemSelected等都與onClick事件類似,讀者可自行實驗。
  • 1
  • 1

我們可以看到,使用ButterKnife之後,我們再也不用findViewById,也不用一個一個設定view.setOnClickListener,是不是很方便。下面我們再來介紹它在fragment中的用法

在Fragment中使用

  • 初始化
 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(getLayoutResId(), container, false);
        mUnbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

再fragment中,除了繫結和解綁方法有些區別不同之外,其他用法與activity中一模一樣。

在介面卡中使用

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);
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

自定義view中使用(不需要指定id)

public class FancyButton extends Button {
  @OnClick
  public void onClick() {
    // TODO do something!
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在這裡ButterKnife的所有用法都介紹完了, 它可以減少我們很多重複的findView操作,使程式碼變得優雅簡潔。有木有感覺 ButterKnife 特別強大。