ButterKnife框架使用详解

来源:互联网 发布:焊接数值模拟软件 编辑:程序博客网 时间:2024/05/01 04:29

一、     什么是ButterKnife

ButterKnife是一个支持View注入的框架。以前总是要写很多findViewById来找到View对象,有了ButterKnife可以很轻松的省去这些步骤。

是大神JakeWharton的力作,目前使用很广。最重要的一点,使用ButterKnife对性能基本没有损失,因为ButterKnife用到的注解并不是在运行时反射的,而是在编译的时候生成新的class。(xUtils的注入都是运行时反射,对性能影响很大。)

ButterKnife目前的最新版本是7.0.1。

 

Butter Knife 的特性

 

    支持 Activity 中的 View 注入

 

    支持 View 中的 View 注入

 

    支持 View 事件回调函数注入

 

目前支持如下事件回调函数:

 

    View: @OnLongClick and @OnFocusChanged.

 

    TextView: @OnEditorAction.

 

    AdapterView: @OnItemClick and @OnItemLongClick.

 

    CompoundButton: @OnCheckedChanged.

二、     使用ButterKnife绑定View

在gradle中添加依赖

compile 'com.jakewharton:butterknife:7.0.1'

 

2.1       在Activity中注入(从7.0.0开始称绑定更合适)

package com.qianfeng.butterknifedemo;  import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Button;import android.widget.TextView;  import butterknife.Bind;import butterknife.ButterKnife;  public class MainActivity extends AppCompatActivity{    //给指定的TextView绑定id    @Bind(R.id.tv)    TextView tv;    //给指定的Button绑定id    @Bind(R.id.btn)    Button btn;      @Override    protected void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                 ButterKnife.bind(this);          tv.setText("哈哈");        btn.setText("呵呵呵");    }}

 

注意:需要绑定的控件不能是private和static的

2.2       在Fragment中绑定

package com.qianfeng.butterknifedemo;  import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;  import butterknife.Bind;import butterknife.ButterKnife;    /** * A simple {@link Fragment} subclass. */  public class BlankFragment extends Fragment{    @Bind(R.id.f_tv)    TextView f_tv;      @Bind(R.id.f_btn)    Button f_btn;      public BlankFragment(){        // Required empty public constructor    }      @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle            savedInstanceState){        // Inflate the layout for this fragment        return inflater.inflate(R.layout.fragment_blank, container, false);    }      @Override    public void onViewCreated(View view, @Nullable Bundle savedInstanceState){
//绑定        ButterKnife.bind(this, view);        f_tv.setText("最新的数据");        f_btn.setText("最新的数据");    }}

 

2.3       在ViewHolder中绑定

    static class ViewHolder {
        @InjectView(R.id.person_name)
        TextView name;
        @InjectView(R.id.person_age)
        TextView age;
        @InjectView(R.id.person_location)
        TextView location;
        @InjectView(R.id.person_work)
        TextView work;
        public ViewHolder(View view) {
            //绑定
            ButterKnife.inject(this, view);
        }
    }

注意:我们在ViewHolder中添加了一个有View参数的构造方法,以后再也不需要在getView方法中findViewById了

 

三、     使用ButterKnife绑定事件回调方法

@OnClick({R.id.btn})  public void ok(){ //方法形参列表中可以有View也可以没有    Toast.makeText(this, "点击了Activity中的按钮", Toast.LENGTH_SHORT).show();  }

 

 

也可以多个View控件的点击事件绑定在一个方法中。

 

除了点击事件@OnClick,还有ListView的点击@OnItemClick, CheckBox的@OnCheckedChanged等等.

 

四、     把多个View放在一起组成View List

@Bind({R.id.btn,R.id.tv})  //把多个View放入List集合中List<View> views;

 

五、     ButterKnife的Apply方法

apply方法可以对指定的list集合中的View进行批量操作

共有三个重载方法:

 

其中Action和Setter是ButterKnife的内部接口。

 

例如:对views集合中所有 的view都会分别指向applay中的方法。

ButterKnife.apply(views, new ButterKnife.Action<View>(){    @Override    public void apply(View view, int index){        TextView tv1 = (TextView) view;        tv1.setText("aabbbaa");    }});

 

ButterKnife.apply(views, new ButterKnife.Setter<View, String>(){      @Override    public void set(View view, String value, int index){      }}, "aaaaa");

 

六、     ButterKnife一些其他常用方法

1、         解除绑定

@Override  protected void onDestroy(){    super.onDestroy();    //解绑。  即把上面绑定的view全部置为null    ButterKnife.unbind(this);    }

 

2、         选择性绑定。

默认情况下绑定View和事件的时候,控件的id是必须存在的,否则会抛出异常。  选择性绑定可以做到如果id存在就绑定,否则不绑定,不抛出任何异常。  想达到这样的想过只需要在@bind 或@onClick前添加@null即可

@Nullable @Bind(R.id.f_tv)TextView tv;

 

3、         多方法绑定

@OnItemSelected(R.id.list_view)

void onItemSelected(int position) {

  // TODO ...

}

 

@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)

void onNothingSelected() {

  // TODO ...

}

4、         findById方法。以前我们调用findViewById需要进行强制类型转换。使用它可以免除强制类型转换,可以自动完成类型转换。

View view = LayoutInflater.from(context).inflate(R.layout.thing, null);

TextView firstName = ButterKnife.findById(view, R.id.first_name);

TextView lastName = ButterKnife.findById(view, R.id.last_name);

ImageView photo = ButterKnife.findById(view, R.id.photo);

 

六、     在Android studio中使用插件自动生成ButterKnife需要的代码

使用插件:AndroidButterKnife Zelezny



 

 

 

 

 

 

 

 

 

3 0
原创粉丝点击