ButterKnife(已過時)

一、ButterKnife(代碼注入框架)的註冊與綁定

在Activity中綁定 :ButterKnife.bind(this)必須在setContentView之後綁定,且父類bind綁定後,子類不需要再bind

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //綁定初始化ButterKnife
        ButterKnife.bind(this);
    }
}

在非Activity(Fragment、ViewHold)中綁定: ButterKnife.bind(this,view),這裏的this不能替換成getActivity

在Activity中不需要做解綁操作,在Fragment中必須在onDestroyView中做解綁操作

public class ButterknifeFragment extends Fragment {
  private Unbinder unbinder;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);
    //返回一個Unbinder值(進行解綁),注意這裏的this不能使用getActivity()  
    unbinder = ButterKnife.bind(this, view);
    return view;
  }

  /**
   * onDestroyView中進行解綁操作
   */
  @Override
  public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }
}

在Adapter中綁定ButterKnife

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

使用ButterKnife修飾的方法和控件,不能用private(如果是private,那麼只能通過反射獲取,影響性能)、static修飾,否則會報錯

setContentView()不能通過註解實現

二、ButterKnife的基本使用

View綁定:

@BindView(R2.id.button)
public Button button;

多個View綁定:

public class MainActivity extends AppCompatActivity {

  @BindViews({R2.id.button1, R2.id.button2, R2.id.button3})
  public List<Button> buttonList;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(this);

    buttonList.get(0).setText("hello 1 ");
    buttonList.get(1).setText("hello 2 ");
    buttonList.get(2).setText("hello 3 ");
  }
}

資源綁定:

綁定String字符串:@BindString( )

綁定string-array數組:@BindArray( )

<resources>
    <string name="app_name">城市</string>
    <string-array name="city">
        <item>北京市</item>
        <item>天津市</item>
        <item>哈爾濱市</item>
        <item>大連市</item>
        <item>香港市</item>
    </string-array>
</resources>

public class MainActivity extends AppCompatActivity {

  @BindView(R2.id.button)//綁定Button控件
  public Button button;

  @BindString(R2.string.app_name)//綁定資源文件中String字符串
  String str;

  @BindArray(R2.array.city)  //綁定string-array數組
  String[] citys;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //綁定activity
    ButterKnife.bind(this);
    button.setText(citys[0]);
  }
}

綁定Bitmap資源:@BindBitmap( )

綁定顏色值:@BindColor( )

事件綁定:

public class MainActivity extends AppCompatActivity {

  @OnClick(R2.id.button1)
  public void showToast() {
    Toast.makeText(this, "is a click", Toast.LENGTH_SHORT).show();
  }

  @OnLongClick(R2.id.button1)
  public boolean showToast2() {
    Toast.makeText(this, "is a long click", Toast.LENGTH_SHORT).show();
    return true;
  }

  @OnClick({R.id.ll_product_name, R.id.ll_product_lilv,
                        R.id.ll_product_qixian, R.id.ll_product_repayment_methods})
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.ll_product_name:
        System.out.print("我是點擊事件1");
        break;
      case R.id.ll_product_lilv:
        System.out.print("我是點擊事件2");
        break;
      case R.id.ll_product_qixian:
        System.out.print("我是點擊事件3");

        break;
      case R.id.ll_product_repayment_methods:
        System.out.print("我是點擊事件4");
        break;
    }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //綁定activity
    ButterKnife.bind(this);
  }
}

另外,ButterKnife在使用時需要設置混淆規則

三、ButterKnife原理:編譯期註解處理、代碼生成工具

R和R2的區別:R.java中聲明的變量不是常量,那我們就copy一份R.java,搞個R2.java,然後把所有變量都加上final關鍵字,然後相關地方直接引用R2不就得了咯

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章