Butter Knife 使用(放弃可恶的findviewbyid)

去掉findViewById以及恶心的类型转化

@BindView(R.id.title)  TextView title;
//TextView title =  (TextView)findViewByID(R.id.title);
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List nameViews;

在View、Activity和Dialog中找到想要View(该方法使用的泛型来对返回值进行转换,因此无需类型转化)

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

作者:545a3c856c5f
链接:http://www.jianshu.com/p/9ad21e548b69
來源:
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

资源绑定

  @BindString(R.string.title) String title;
  @BindDrawable(R.drawable.graphic) Drawable graphic;
  @BindColor(R.color.red) int red; 
  @BindDimen(R.dimen.spacer) Float spacer; 

Fragment中:

ViewHolder中:(这里有个万能适配器封装,大大简化代码)

static class ViewHolder {
        @BindView(R.id.title)
        TextView name;
        @BindView(R.id.job_title) TextView jobTitle;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

原文超详细

你可能感兴趣的:(Butter Knife 使用(放弃可恶的findviewbyid))