zoukankan      html  css  js  c++  java
  • ButterKnife注解式绑定控件

    Butter Knife Android为控件设计的注解绑定库。

    github地址:https://github.com/JakeWharton/butterknife

    添加依赖:(具体看github官网)

    dependencies {
      implementation 'com.jakewharton:butterknife:9.0.0-rc2'
      annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc2'
    }

    注意:·修饰类型不能是:private 或者 static;

       ButterKnife.bind(this);必须在setContentView();之后绑定;且父类bind绑定后,子类不需要再bind。

       在非Activity中,this不能替换成getActivity()。

       在Activity中不需要做解绑操作,在Fragment 中必须在onDestroyView()中做解绑操作。

    ·//绑定activity
    ButterKnife.bind(this);

    ·//绑定fragment
    ButterKnife.bind( this , view ) ;
    ·//解除绑定
    ButterKnife.unbind(this);
    ·//多个控件id 注解
    @BindViews({ R.id.button1 , R.id.button2 , R.id.button3 }public List<Button> buttonList ;

    ·//绑定string 字符串
    @BindString( R.string.app_name )  public String str;
    ·//绑定string里面array数组
    @BindArray(R.array.city );
    ·//绑定Bitmap 资源
    @BindBitmap( R.mipmap.wifi )
    ·//绑定一个颜色值
    @BindColor( R.color.colorAccent )
    ·//设置一个点击事件
    @OnClick(R.id.button1 )
    ·//设置一个长按事件
    @OnLongClick(R.id.button1)

     在Adapter的ViewHolder中使用,将ViewHolder加一个构造方法,在new ViewHolder的时候把view传递进去。使用ButterKnife.bind(this, view)进行绑定,代码如下:

    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.testlayout, parent, false);  
          holder = new ViewHolder(view);  
          view.setTag(holder);  
        }  
    
        holder.name.setText("Donkor");  
        holder.job.setText("Android");
        // etc...  
        return view;  
      }  
    
      static class ViewHolder {  
        @BindView(R.id.title) TextView name;  
        @BindView(R.id.job) TextView job;  
    
        public ViewHolder(View view) {  
          ButterKnife.bind(this, view);  
        }  
      }  
    }
  • 相关阅读:
    USB Device Finder
    Delphi Interfaces
    Why we need interfaces in Delphi
    PostQuitMessage, PostThreadMessage( WM_QUIT )
    state与status的区别
    SQLSERVER监控复制并使用数据库邮件功能发告警邮件
    干货分享:SQLSERVER使用裸设备
    SQLSERVER truncate table之后是否会重置表的自增值
    配置SQL Server去使用 Windows的 Large-Page/Huge-Page allocations
    SQLSERVER 数据库性能的的基本
  • 原文地址:https://www.cnblogs.com/leizz/p/10117973.html
Copyright © 2011-2022 走看看