当前位置:主页 > 查看内容

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

发布时间:2021-07-26 00:00| 位朋友查看

简介:想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://harmonyos.51cto.com/#zz harmony-view-binding 最新版本:Gitee仓库查看 是什么? - view-binding for harmony - 鸿蒙应用开发view-binding插件,消除findComponentById模版代……

想了解更多内容,请访问:

51CTO和华为官方战略合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

harmony-view-binding

最新版本:Gitee仓库查看

是什么?

- view-binding for harmony

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

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

怎么用?

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

  1. buildscript { 
  2.     repositories { 
  3.         maven { 
  4.             url 'https://mirrors.huaweicloud.com/repository/maven/' 
  5.         } 
  6.         maven { 
  7.             url 'https://developer.huawei.com/repo/' 
  8.         } 
  9.  
  10.         jcenter() 
  11.         maven{ 
  12.             url 'https://dl.bintray.com/eholee/maven' 
  13.         } 
  14.     } 
  15.     dependencies { 
  16.         classpath 'com.huawei.ohos:hap:2.4.0.1' 
  17.         // view-binding 
  18.         classpath 'com.eholee.plugin:view-binding:1.0.1' 
  19.     } 

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

  1. apply plugin: 'com.huawei.ohos.hap' 
  2.  
  3. apply plugin: 'com.eholee.plugin.view-binding' 
  4.  
  5. ohos { 
  6.  
  7. ... 
  8.  
  9.  
  10. viewBinding{ 
  11.  
  12. enable true 
  13.  
  14.  
  15. dependencies { 
  16.  
  17. ... 
  18.  

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()

  1. public class MainAbilitySlice extends AbilitySlice { 
  2.  
  3. private AbilityMainBinding binding; 
  4.  
  5. @Override 
  6.  
  7. public void onStart(Intent intent) { 
  8.  
  9. super.onStart(intent); 
  10.  
  11. binding = AbilityMainBinding.parse(this); 
  12.  
  13. super.setUIContent(binding.getRoot()); 
  14.  
  15. binding.textHelloworld.setClickedListener(new Component.ClickedListener() { 
  16.  
  17. @Override 
  18.  
  19. public void onClick(Component component) { 
  20.  
  21. new ToastDialog(MainAbilitySlice.this).setText("click").show(); 
  22.  
  23.  
  24. }); 
  25.  
  26.  
  27. @Override 
  28.  
  29. public void onActive() { 
  30.  
  31. super.onActive(); 
  32.  
  33.  
  34. @Override 
  35.  
  36. public void onForeground(Intent intent) { 
  37.  
  38. super.onForeground(intent); 
  39.  
  40.  

可选项

1. 提供设置根布局api

  1. parse(Context context, ComponentContainer parent, boolean attachToRoot)   

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

feature中的build.gradle中设置

  1. viewBinding{ 
  2.         enable false  
  3.         // false为关闭,插件将不会解析该feature所有的xml布局文件, 
  4.         //true为开启,插件将会解析该feature下所有的xml布局文件 
  5. }  

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

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

  1. xmlns:eholee="http://schemas.eholee.com/viewbinding" 
  2.  eholee:view_binding="false" 
  3.  示例: 
  4.  <?xml version="1.0" encoding="utf-8"?> 
  5.  <DirectionalLayout 
  6.       xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  7.       xmlns:eholee="http://schemas.eholee.com/viewbinding" 
  8.       eholee:view_binding="false" 
  9.       ohos:height="match_parent" 
  10.       ohos:width="match_parent" 
  11.       ohos:background_element="$color:colorAppBackground" 
  12.       ohos:orientation="vertical"
  13.       ... 
  14.   </DirectionalLayout>  

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技术社区共同所有,如需转载,请注明出处,否则将追究法律责任.

想了解更多内容,请访问:

51CTO和华为官方战略合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz


本文转载自网络,原文链接:https://harmonyos.51cto.com/#zz
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐