华为鸿蒙小插件,鸿蒙Harmony应用开发view-binding插件,和findComponentById说再见

d7a7af7ec73e922159be1d2cf2cba3ea.png

harmony-view-binding

最新版本:Gitee仓库查看

是什么?

- view-binding for harmony

- 鸿蒙应用开发view-binding插件,消除findComponentById模版代码

- 无注解、编译期间生成Binding类文件

怎么用?

1. 在project根目录的build.gradle文件中引入view-binding的maven仓库地址和classpath

buildscript {

repositories {

maven {

url 'https://mirrors.huaweicloud.com/repository/maven/'

}

maven {

url 'https://developer.huawei.com/repo/'

}

jcenter()

maven{

url 'https://dl.bintray.com/eholee/maven'

}

}

dependencies {

classpath 'com.huawei.ohos:hap:2.4.0.1'

// view-binding

classpath 'com.eholee.plugin:view-binding:1.0.1'

}

}

2. 在feature模块的build.gradle文件中引入view-binding插件

apply plugin:'com.huawei.ohos.hap'

apply plugin: 'com.eholee.plugin.view-binding'

ohos {

...

}

viewBinding{

enable true

}

dependencies {

...

}

3. 执行gradle sync 即可自动生成ViewBinding类,生成目录在feature中的build/generated/source/viewBinding中,

类的命名方法通过获得xml布局文件名后遵循大驼峰法(Upper Camel Case)并追加Binding后缀,如:MainAbilityBinding

4. 在需要填充布局的地方使用

主要是两个api:1. binding = AbilityMainBinding.parse(this); 2. binding.getRoot()

publicclass MainAbilitySlice extends AbilitySlice {

private AbilityMainBinding binding;

@Override

publicvoid onStart(Intent intent) {

super.onStart(intent);

binding = AbilityMainBinding.parse(this);

super.setUIContent(binding.getRoot());

binding.textHelloworld.setClickedListener(new Component.ClickedListener() {

@Override

publicvoid onClick(Component component) {

new ToastDialog(MainAbilitySlice.this).setText("click").show();

}

});

}

@Override

publicvoid onActive() {

super.onActive();

}

@Override

publicvoid onForeground(Intent intent) {

super.onForeground(intent);

}

}

可选项

1. 提供设置根布局api

parse(Context context, ComponentContainer parent, boolean attachToRoot)

2. 支持feature模块view-binding功能的开启与关闭:

feature中的build.gradle中设置

viewBinding{

enable false

// false为关闭,插件将不会解析该feature所有的xml布局文件,

//true为开启,插件将会解析该feature下所有的xml布局文件

}

3. 支持针对单个xml布局文件开启与关闭view-binding功能

默认是都开启,如需关闭,需在xml根节点中加入如下信息:

xmlns:eholee="http://schemas.eholee.com/viewbinding"

eholee:view_binding="false"

示例:

xmlns:ohos="http://schemas.huawei.com/res/ohos"

xmlns:eholee="http://schemas.eholee.com/viewbinding"

eholee:view_binding="false"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:background_element="$color:colorAppBackground"

ohos:orientation="vertical">

...

Gitee仓库地址:https://gitee.com/jeffer_s/harmony-view-binding

参考

1. Android ViewBinding

2. com.huawei.ohos:hap:2.4.0.1 插件api

LICENSE

Apache License 2.0

©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任.

【编辑推荐】

【责任编辑:jianghua TEL:(010)68476606】

点赞 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值