Android Butterknife使用

前言

目前写大创的东西,做Android方面的coding。在参考学长写的项目的时候,看见了这个 butterKnife 框架。想到当初他们有推荐过,目前也确实需要,所以就边学边用了。参考Android Butter Knife 框架——最好用的View注入官方文档。本人英语较渣,欢迎有余力的同学也去看看官方文档。

Butter Knife

Butter Knife,是专门为 View 设计的绑定注释,解决需要大量 findViewByID 代码量大的问题。

简介

通过 @BindView 注释,并传入一个 View 的 ID,Butter Knife 就能去找到并自动绑定 layout 中对应的 View

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...
 }
}

代码的生产被用来执行 View 的查询,而不再是缓慢的反射。调用 bind 来生成代码,你可以打开它进行调试。上面生成的代码大致等于如下代码

public void bind(ExampleActivity activity) {
  activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578);
  activity.footer = (android.widget.TextView) activity.findViewById(2130968579);
  activity.title = (android.widget.TextView) activity.findViewById(2130968577);
}

资源绑定

可以使用 @BindBool,@BindColor,@BindDimen,@BindDrawable,@BindInt,@BindString这些绑定注释,通过传入相应的 ID 来 绑定相应的预定义资源

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
  // ...
}

非 Activity 中绑定

Butter Knife 中 bind 有多个重载,只要传入相应的布局,在任何你自己的视图中都能使用。

例如 Fragment 如下

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;
  }
}

另一种使用是用来简化 list 适配器的过程(Another use is simplifying the view holder pattern inside of a list adapter. 对 Adapter 还不是特别熟练,所以直接将原文贴在这里了 )

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.bind 在任何你想使用 findViewById 的地方

其他提供的绑定 API:

  • 使用一个 Activity 作为 View root 绑定任意对象。如果你使用像 MVC 模式,你可以通过 Controller 所在的 Activity ,使用 ButterKnife.bind(this,activity) 来绑定 Controller
  • 使用ButterKnife.bind(this)绑定一个布局的子布局。如果你在布局中使用了<merge>标签并且在自定义的控件构造时 inflate 这个布局,你可以在 inflate 之后立即调用它。或者,你可以在onFinishInflate()回调中使用它。

View 列表

你可以将多个 View 放在列表或者数组中

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

apply 函数能够允许你在一个列表中执行操作

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值