HarmonyOS DataBinding 使用指南
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
在開(kāi)始講DataBinding之前,我們不得不先說(shuō)一下MVVM架構(gòu)模式,MVVM是MVP模式的改進(jìn)版,Model層跟View層與MVP模式類似,ViewModel層只做和邏輯處理相關(guān)的工作,在ViewModel中不會(huì)持有View層的引用,這時(shí)候就需要借助DataBinding,通過(guò)Binding方式通信,只需要在ViewModel層對(duì)數(shù)據(jù)進(jìn)行操作,View層就會(huì)自動(dòng)更新UI。
概述
Databinding 顧名思義就是數(shù)據(jù)綁定,HarmonyOS為提供了Databinding庫(kù),該庫(kù)允許你使用聲明格式而不是以代碼的方式將數(shù)據(jù)綁定到UI上。Databinding庫(kù)會(huì)解析布局文件,自動(dòng)生成數(shù)據(jù)綁定代碼,實(shí)現(xiàn)數(shù)據(jù)源與UI組件之間的相互綁定。
自動(dòng)生成綁定代碼的基類,是用來(lái)實(shí)現(xiàn)ComponentContainer和ActiveData對(duì)象之間的綁定,ComponentContainer是指組件容器,相當(dāng)于Android的ViewGroup,ActiveData是一個(gè)可觀察數(shù)據(jù)類,同時(shí)也具有生命周期感知,作用類似于Android的LiveData。
當(dāng)ComponentContainer或ActiveData對(duì)象被修改時(shí),DataBinding對(duì)象會(huì)自動(dòng)修改綁定到ComponentContainer或ActiveData的對(duì)象。比如,如果你修改了某個(gè)ComponentContainer的屬性,DataBinding會(huì)將屬性值到綁定到該ComponentContainer的ActiveData對(duì)象。反之亦然,如果一個(gè)ActiveData對(duì)象的屬性值被更改,綁定的ComponentContainer的屬性值也將被更新。
開(kāi)始使用
1.在使用DataBinding之前,首先要在應(yīng)用模塊下build.gradle中開(kāi)啟dataBinding,代碼如下:
- ohos {
- ...
- buildTypes {
- debug {
- dataBindingEnabled true
- }
- }
- }
2.使用DataBinding之前,首先使用ActiveData對(duì)象來(lái)定義要觀察的數(shù)據(jù),并實(shí)現(xiàn)其0get() 和set()方法:
創(chuàng)建一個(gè)Model類
- public class MainAbilityModel {
- private ActiveData titile;
- public ActiveData getTitile() {
- return titile;
- }
- public void setTitile(ActiveData titile) {
- this.titile = titile;
- }
- }
3.在我們的布局文件中,聲明DataBinding的綁定標(biāo)簽
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:background_element="#1a1a1a"
- ohos:orientation="vertical">
- <Text
- ohos:id="$+id:title_text"
- ohos:height="300"
- ohos:width="match_parent"
- ohos:text="${model.titile}"
- ohos:text_alignment="center"
- ohos:text_color="#FF555555"
- ohos:text_size="50"/>
- <....>
- <binddata>
- <variable
- class="com.example.time.model.MainAbilityModel"
- name="model"/>
- </binddata>
- </DirectionalLayout>
在<Text>
4.在布局中聲明DataBinding后,系統(tǒng)會(huì)在編譯后自動(dòng)生成一個(gè)以布局文件命名的Binding類,比如我的布局文件名為ability_main,那么系統(tǒng)就會(huì)自動(dòng)生成一個(gè)AbilityMainBinding類。我們?cè)赟lice類中調(diào)用DataBindingUtil.createBinding方法來(lái)獲取AbilityMainBinding的對(duì)象,然后調(diào)用initComponent及setLifecycle來(lái)初始化對(duì)象,之后我們就可以調(diào)用在ActiveData對(duì)象設(shè)置數(shù)據(jù),調(diào)用MainAbilityModel中定義的方法,綁定到其中的ActiveData對(duì)象。
- public class MainAbilitySlice extends AbilitySlice {
- private static HiLogLabel mLabel = new HiLogLabel(HiLog.LOG_APP, 00001, "suisui");
- AbilityMainBinding binding;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- ComponentContainer componentContainer =
- (ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ability_main, null, false);
- if (!(componentContainer instanceof ComponentContainer)) {
- return;
- }
- super.setUIContent(componentContainer);
- try {
- binding = DataBindingUtil.createBinding(ResourceTable.Layout_ability_main, getContext(), "com.example.time");
- } catch (IllegalArgumentException | IOException exception) {
- HiLog.info(mLabel, exception.toString());
- }
- if (binding != null) {
- binding.initComponent(componentContainer);
- binding.setLifecycle(getLifecycle());
- ActiveData price = new ActiveData<>();
- price.setData("DataBinding Demo");
- MainAbilityModel model = new MainAbilityModel();
- model.setTitile(price);
- binding.setModel(model);
- }
- }
- }
需要注意的是在調(diào)用DataBindingUtil.createBinding時(shí),要替換成自己的包名。
至此,我們大致的把DataBinding的簡(jiǎn)單使用梳理了,總體來(lái)說(shuō)DataBinding可以為我們減少代碼量,也不需要再做findComponentById,設(shè)置數(shù)據(jù)等一些繁瑣的操作。但在實(shí)際業(yè)務(wù)開(kāi)發(fā)當(dāng)中可能也會(huì)有一定的局限性,例如ActiveData的類型轉(zhuǎn)換問(wèn)題,相信后續(xù)官方也會(huì)越來(lái)越完善,HarmonyOS也會(huì)越來(lái)越好,讓我們拭目以待。
最終效果

注:DevEco Studio版本過(guò)低可能會(huì)導(dǎo)致編譯生成Binding找不到包,可升級(jí)版本再試。
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)