ButterKnife使用方法详解

介绍

ButterKnife是一个专注于Android系统的View注入框架,以前总是要写很多findViewById来找到View对象,有了ButterKnife可以很轻松的省去这些步骤。使用ButterKnife对性能基本没有损失,因为ButterKnife用到的注解并不是在运行时反射的,而是在编译的时候生成新的class。

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

原理

利用了IOC的(Inverse of Controll)控制反转结构,2004年后改名为DI(dependency injection)依赖注入。目的是为了使类与类之间解耦合,提高系统的可扩展性和可维护性。越来越趋向于后端开发了。

配置

在Android Studio项目中配置使用ButterKnife
本文介绍使用的as版本为3.1.2,ButterKnife版本为8.8.1
1.如果你是直接在app中使用,只需在app的 build.gradle 中添加如下代码:

dependencies {
  implementation 'com.jakewharton:butterknife:8.8.1'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
 
 
  • 1
  • 2
  • 3
  • 4

2.如果你是在Library库中使用,按照github上的配置来写,会报错。

Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)</li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)</li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
 
 
  • 1
  • 2
  • 3
  • 4

在网上找到了大神的解决办法解决组件化开发butterknife 在 library中使用的坑
第一步
配置项目根目录的build.gradle
这里写图片描述
这里注意butterknife的版本,我从8.8.1一直往下降,到了8.5.1才可以使用

第二步
配置library中的build.gradle
顶部添加插件apply plugin: ‘com.jakewharton.butterknife’
这里写图片描述
在dependencies中添加和在app中的一样
这里写图片描述
到此配置已完成,下面说下在library中使用时注意事项

1.用R2代替R,这点butterknife的开发者也做了说明

@BindView(R2.id.test_tv)
TextView tv;
@BindView(R2.id.test_btn)
Button btn;
 
 
  • 1
  • 2
  • 3
  • 4

2.onclick事件,在library中不能用switch,如果使用,点击事件会失效,用if和else代替。还要注意id的使用
这里写图片描述

安装插件

这里写图片描述
安装后重启生效
使用时,右键layout->点击Generate->Generate ButterKnife Injections
这里写图片描述
这里写图片描述

注解使用用例

绑定id:BindView

@BindView(R.id.login_btn_login)
Button btn_login;
 
 
  • 1
  • 2

Adapter中ViewHolder使用,注意:是this

public MyViewHolder(View itemView){
        super(itemView);
        ButterKnife.bind(this, itemView);
}
 
 
  • 1
  • 2
  • 3
  • 4

Button点击事件:@OnClick

@OnClick({R.id.login_btn_login})
public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_btn_login:

                break;
            default:
                break;
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

CheckBox点击事件:@OnCheckedChanged

@OnCheckedChanged({R.id.login_cb_remember_password, R.id.login_cb_auto_login})
public void onViewCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        switch (compoundButton.getId()) {
            case R.id.login_cb_remember_password:
                if (isChecked) {

                } else {

                }
                break;
            case R.id.login_cb_auto_login:
                if (isChecked) {

                } else {

                }
                break;
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

RadioButton点击事件:@OnClick

@OnClick({R.id.rb_man, R.id.rb_woman})
public void onViewCheckedChanged(RadioButton radioButton) {
        boolean checked = radioButton.isChecked();
        switch (radioButton.getId()) {
            case R.id.rb_man:
                if (checked) {

                }
                break;
            case R.id.rb_woman:
                if (checked) {

                }
                break;
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

ViewPager事件监听:@OnPageChange

@OnPageChange(R.id.viewpager)
public void onPageSelected(int position) {
        switch (position) {
            case 0:

                break;
            case 1:

                break;
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

转载地址:https://blog.csdn.net/zyw0101/article/details/80399225
Demo下载地址:ButterKnifeDemo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值