ButterKnife基本使用

ButterKnife框架使用可以方便我們不用寫大量的重復(fù)繁瑣的findViewById和setOnClickListener等代碼,它采用依賴注入的方式,通過注解的方式讓view和代碼中的對象綁定起來。

logo.png

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

ButterKnife使用

  • 添加依賴

由于8.0.0之后的版本和之前的版本有差異,這里主要是8.1.0最新版本的添加方法,注意兩個步驟都要完成:

1.Project的build.gradle中添加:
    dependencies { 
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
2.App的build.gradle中添加:
    apply plugin: 'com.neenbedankt.android-apt'

    dependencies {
        compile 'com.jakewharton:butterknife:8.1.0'
        apt 'com.jakewharton:butterknife-compiler:8.1.0'
    }
  • Activity中使用

      public class MainActivity extends AppCompatActivity {
          @BindView(R.id.title_tv) TextView titleTv;
          @BindView(R.id.back_btn) Button backBtn;
          @BindView(R.id.logo_img) ImageView logoImg;
          
          @Override 
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              ButterKnife.bind(this);
          }
      }
    

注意:ButterKnife.bind(this)必須在setContentView之后。

  • Fragment中使用

      public class ListFragment extends Fragment{
          @BindView(R.id.title_tv) TextView titleTv;
          @BindView(R.id.listview) ListView listView;
          private Unbinder unbinder;
          @Override 
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              View view = inflater.inflate(R.layout.fragment_list, container, false);
              unbinder = ButterKnife.bind(this, view);
              return view;
          }
          @Override
          public void onDestroyView() {    
              super.onDestroyView();    
              unbinder.unbind();
          }
      }
    

由于Fragment的生命周期不同于Activity,當(dāng)在CreateView方法中綁定視圖時,需要在onDestoryView中把對應(yīng)的視圖設(shè)置為null,這時需要解綁ButterKnife。

  • ViewHolder中使用

      static class ViewHolder {
          @BindView(R.id.name_tv) TextView nameTv;
          @BindView(R.id.content_tv) TextView contentTv;
          @BindView(R.id.head_img) ImageView headImg;
    
          public ViewHolder(View view) {
              ButterKnife.bind(this, view);
          }
      }
    
  • 事件監(jiān)聽綁定

      //點(diǎn)擊事件
      @OnClick(R.id.submit)
      public void buttonClick(Button button){
          //TODO ...
      }
    
      //listview item點(diǎn)擊事件
      @OnItemClick(R.id.listview)
      public void itemClick(ListView listView){
          //TODO ...
      }
    
      //多個控件具有相同的點(diǎn)擊事件
      @OnClick({ R.id.btn1, R.id.btn2, R.id.btn3 })
      public void buttonsClick(Button button){
          //TODO ...
      }
    

ps:方法中的參數(shù)是可選的,但如果存在,必須是這個控件類或者控件類的父類。

  • 資源綁定

可以用@BindBool,@BindColor,@BindDimen,@BindDrawable,@BindInt和@BindString通過綁定R.bool以及其他對應(yīng)id來進(jìn)行資源的預(yù)定義。
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.graphic) Drawable graphic;
@BindColor(R.color.red) int red;
@BindDimen(R.dimen.spacer) Float spacer;
//...
通過這種方式,就可以把資源直接賦值給變量,從而不再需要初始化。

  • 可選綁定

默認(rèn)情況下,@bind和監(jiān)聽器綁定都必須有一個目標(biāo)view,當(dāng)butter knife找不到對應(yīng)的view時會拋出一個異常。為了防止這種異常情況的發(fā)生,可以在綁定的字段前面使用@Nullable注解,在綁定的方法前面則可使用@Option注解,來表明對應(yīng)的是一個可選綁定。

注:任何名為@Nullable第三方的注解都可以對字段起作用,這里推薦使用Android的”support-annotations“ library提供的@Nullable注解。
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing)
void onMaybeMissingClicked() {
// TODO ...
}

  • 其他

butter knife也提供了一個findById方法,如果要在某些情況下查找某些子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);

  • 混淆

    -keep class butterknife.** { *; }
    -dontwarn butterknife.internal.**
    -keep class **$$ViewBinder { *; }
    -keepclasseswithmembernames class * {
        @butterknife.* <fields>;
    }
    -keepclasseswithmembernames class * {
        @butterknife.* <methods>;
    }
    

參考資料

官方文檔:https://jakewharton.github.io/butterknife/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容