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);  
        }  
      }  
    }
  • 相关阅读:
    python面试题解析(python基础篇80题)
    python面试题
    网络IO模型 主要 IO多路复用
    线程队列 线程池 协程
    线程的Thread模块 同步控制:锁,事件,信号量,条件,定时器
    进程池,线程的理论,Threading.Thread来创建线程
    进程之间的通信(IPC),对列,管道,数据共享.进程池初识
    Process 进程之间的数据隔离问题,守护进程,锁,信号量,事件
    js get the local domain and path fast
    github jekyll blog
  • 原文地址:https://www.cnblogs.com/leizz/p/10117973.html
Copyright © 2011-2022 走看看