studio 3.0版本使用ButterKnife

Butter Knife的优点

1) 强大的View绑定和Click事件处理功能,简化代码,提升开发效率 

2)方便的处理Adaper里的ViewHolder绑定问题 

3) 运行时不会影响app效率,使用配置方便 

4)代码清晰,可读性强

具体使用

1.第一步:添加依赖

implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'com.jakewharton:butterknife-compiler:8.8.1'

2.第二步:
在module的build.gradle中添加如下代码
需要添加在Android目录下的defaultconfig中添加

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        //在这里添加
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath = true
            }
        }
    }


}

3.在MainActicity里绑定

 ButterKnife.bind(this);

4.在Fragment里使用

  @BindView(R.id.login)
    TextView login;
    Unbinder unbinder;
 unbinder = ButterKnife.bind(this, view);

记得在onDestroyView解绑

   @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

5.在adapter里使用在ViewHolder里初始化

  @BindView(R.id.home_classifiled_text3)
        TextView homeClassifiledText3;

        public ViewHolderB(View itemView) {
            super(itemView);
            //初始化ButterKnife
            ButterKnife.bind(this, itemView);

        }

如果没有安装插件的朋友可以参考这个链接内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值