Android之ButterKnife用法详解

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

相信很多开发Android的小伙伴,都厌倦了findViewById(),都是基本重复的操作,所以我们可以使用依赖注入框架来偷懒。目前,用的较多的两种大概是ButterKnife和dagger,英文译为黄油刀和匕首,听名字就很酷。

今天我们就来详细介绍ButterKnife的用法

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

GitHub地址

## 添加依赖 * 首先在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 特别强大。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值