Android ----- butterknife框架使用

介绍

butterknife也是一个依赖注入框架,借助annonation实现view的快速初始化,解除findViewById的烦恼

工程引入

配置项目根目录的build.gradle,来引入android-apt插件

1
2
3
4
5
6
7
8
buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

配置app的build.gradle

dependencies {
compile 'com.jakewharton:butterknife-compiler:8.5.1'
}

配置module的build.gradle,apply the android-apt plugin,and add the Butter Knifedependencies.

1
2
3
4
5
6
7
8
9
10
11

apply plugin: 'android-apt`

android{
...
}

dependencies{
 compile 'com.jakewharton:butterknife:8.2.1'
 apt 'com.jakewharton:butterknife-compiler:8.2.1'
}

注意
butter knife尽量不要在library中使用,如果要用,还得转R2,太麻烦了

使用


打开 Android  studio 左上角File-settings-选择Plugins搜索Android ButterKnife Zelezny然后安装重新启动android studio
setContentView(R.layout.activity_main);
在activity_main中右键Generate选择Generate ButterKnife Injections

使用@BindView注解view

1
2
3
4
5
6
7
8
9
10
11
12
class ExampleActivity extends Activity {
  @BindView(R.id.title) TextView title;
  @BindView(R.id.subtitle) TextView subtitle;
  @BindView(R.id.footer) TextView footer;

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

Resource的注解

@BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString

1
2
3
4
5
6
7
class ExampleActivity extends Activity {
  @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
  // ...
}

non-activity的注解

1
2
3
4
5
6
7
8
9
10
11
public class FancyFragment extends Fragment {
  @BindView(R.id.button1) Button button1;
  @BindView(R.id.button2) Button button2;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    ButterKnife.bind(this, view);
    // TODO Use fields...
    return view;
  }
}

也可以在ViewHolder中使用

View List

1
2
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;

使用apply方法,能一次操作整个View的List

1
2
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);

包括设置属性

1
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);

Listener的绑定

1
2
3
4
@OnClick(R.id.submit)
public void submit(View view) {
  // TODO submit data to server...
}

绑定的释放

由于Fragment的生命周期区别于activity,当我们在Fragment的onCreatView做绑定的时候,要在onDestoryView中设置views为null。Butter Knife 返回一个Unbider实例来解决这个问题,
eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class FancyFragment extends Fragment {
  @BindView(R.id.button1) Button button1;
  @BindView(R.id.button2) Button button2;
  private Unbinder unbinder;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    unbinder = ButterKnife.bind(this, view);
    // TODO Use fields...
    return view;
  }

  @Override public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }
}

更懒的方式


参考:http://www.jianshu.com/p/9ad21e548b69

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值