Android ButterKnife(Library)在組件化的項(xiàng)目中使用

ButterKnife的官方項(xiàng)目的地址:https://github.com/JakeWharton/butterknife
ButterKnife的使用說明:http://jakewharton.github.io/butterknife/
ButterKnife的API文檔:http://jakewharton.github.io/butterknife/javadoc/

ButterKnife由國(guó)外的jakewharton大神寫的,主要作用是注入View獲取控件的Id,以此減小代碼的編寫量。

以下只介紹在模塊化的Library中的使用:
首先在Android Studio的Plugins下搜索


image.png

點(diǎn)擊安裝,然后重啟Android Studio

1.在project的build.gradle下添加如下代碼:

buildscript {
    repositories {
        google()
        jcenter()
        jcenter {
            url "http://jcenter.bintray.com/"
        }
        mavenCentral() // add repository
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-rc1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

ButterKnife最新的版本是10.2.0,添加這個(gè)版本需要兼容android.x,我的項(xiàng)目暫時(shí)沒有兼容Android.x所以我添加的是9.0.0。ButterKnife的版本不能低于8.4.0(包含8.4.0),實(shí)際上我在我的項(xiàng)目中添加8.5.1和8.8.1都不行。

2.在最底層的library的build.gradle里邊添加:

apply plugin: 'com.jakewharton.butterknife'

在dependencies下添加ButterKnife的依賴:

    api rootProject.ext.dependencies["butterKnife0"]
    annotationProcessor rootProject.ext.dependencies["butterKnife1"]

這里我使用了依賴的同一版本管理,其中這里ButterKnife的版本對(duì)應(yīng)的是8.8.1

3.在module的build.gradle的dependencies下添加ButterKnife的依賴:

annotationProcessor rootProject.ext.dependencies["butterKnife1"]

以上的版本管理等同于以下的:

com.jakewharton:butterknife:8.8.1;
com.jakewharton:butterknife-compiler:8.8.1;

同步sync now即可:注意:其中在Library中除了onViewClicked()方法體中的控件id的R不是R2,其他的地方全部是R2來獲取控件id,原因是Libraray下的R需要時(shí)常量,而之前版本的ButterKnife時(shí)沒有兼容這點(diǎn),后來的版本加如了R2來兼容。而在Library以外的地方還是正常給使用!

在Activity中的使用:

public class UserActivity extends AppCompatActivity{

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.backBtn)
    AppCompatImageView backBtn;
    @BindView(R.id.searchUserBtn)
    AppCompatTextView searchUserBtn;
    @BindView(R.id.userList)
    RecyclerView userList;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
         ButterKnife.bind(this);

    }
}

ButterKnife.bind(this);必須在設(shè)置布局View之后。鼠標(biāo)放到R.layout.xxx上,按Alt+Insert鍵,點(diǎn)擊下圖紅色選項(xiàng)即可。


image.png

彈出下面的彈窗,選擇你要進(jìn)行的操作即可:


image.png

其中ButterKnife.bind(this);可以寫在BaseActivity中,子類中就不用再寫

在Fragment中的使用

在onCreateView()方法中調(diào)用 unbinder = ButterKnife.bind(this, mView);返回一個(gè)Unbinder unbinder對(duì)象,在onDestroyView()方法中調(diào)用unBinder.unbind();方法解綁;其他通Activity中的用法。相關(guān)更多Api請(qǐng)查看官方文檔!

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

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

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