美文网首页
ButterKnife使用详解

ButterKnife使用详解

作者: 谷哥的小弟 | 来源:发表于2016-11-24 15:57 被阅读0次

版权声明

原创作者:谷哥的小弟
博客地址:http://blog.csdn.net/lfdfhl

参考资料

准备工作

  • 为Android Studio安装插件Butterknife Zelezny
  • 在build.gradle的dependencies{}中加入

compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

使用方式

  1. 绑定控件
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...
     }
}
  1. 绑定资源
    @BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString.
class ExampleActivity extends Activity {
     @BindString(R.string.title) String title;
     @BindDrawable(R.drawable.graphic) Drawable graphic;
     @BindColor(R.color.red) int red; 
     @BindDimen(R.dimen.spacer) Float spacer; 
     // ...
}
  1. 绑定Fragment
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 b) {
       View view = inflater.inflate(R.layout.fragment, container, false);
       unbinder = ButterKnife.bind(this, view);
       // TODO Use fields...
       return view;
     }
   
     @Override public void onDestroyView() {
       super.onDestroyView();
       unbinder.unbind();
     }
}

在Fragment的onCreateView()中设置绑定,onDestroyView()中解绑

  1. 绑定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);
       }
     }
}
  1. 绑定Click事件监听
@OnClick(R.id.textView)
   public void clickView(){
       Toast.makeText(this,"click",Toast.LENGTH_SHORT).show();
   }

请注意方法应该是public或者static的

相关文章

  • Android常用之Butterknife使用详解

    Android常用之Butterknife使用详解 使用Butterknife很久了,一直没有写过文章,今天记录一...

  • ButterKnife使用详解

    (1)ButterKnife是什么? 在开发过程中,我们总是会写大量的findViewById和点击事件,像初始v...

  • ButterKnife使用详解

    前言 ButterKnife是控件注入框架,可以帮助安卓开发者省去初始化控件的重复性工作,简单快捷地初始化布局文件...

  • ButterKnife使用详解

    注解库, JakeWharton大神的杰作 Note:文章基于8.5.1版本,不同的版本之间用法可能会稍有差异,实...

  • Butterknife使用详解

    项目地址 GitHub地址:https://github.com/JakeWharton/butterknife ...

  • ButterKnife使用详解

    版权声明 原创作者:谷哥的小弟博客地址:http://blog.csdn.net/lfdfhl 参考资料 http...

  • ButterKnife使用详解

    写在前面:该文档使用7.0版本,8.0版本方法名有所改动,建议看官方文档,整体业务逻辑和原理没什么变动,官网 在A...

  • ButterKnife使用详解

    1.在app目录的gradle中添加 2.(1)在Activity的onCreate方法中的setContentV...

  • ButterKnife使用详解

    http://blog.csdn.net/itjianghuxiaoxiong/article/details/5...

  • ButterKnife使用详解

    以前的话经常使用findViewById()来绑定布局中ID,这样写不仅麻烦而且还没有什么效率,今天推荐一款由Ja...

网友评论

      本文标题:ButterKnife使用详解

      本文链接:https://www.haomeiwen.com/subject/dnwbpttx.html