DataBinding基本使用(1)

本文都是接合其他作者的總結(jié),再加上自己實(shí)際操作中遇到的一些問(wèn)題,來(lái)綜合編寫。

DataBinding的配置

因?yàn)榫W(wǎng)上一簍筐,就直接上代碼

android {
   ...
    //核心配置代碼在這里
    dataBinding {
        enabled = true
    }
}

就這樣三行代碼,DataBinding就配置好了。 真的是So easy 媽媽那什么~~~

四個(gè)標(biāo)簽的使用

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <import type="android.view.View"></import>
        <variable
            name="tv1data"
            type="String"></variable>
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.scs.myapplication.MainActivity">
    </LinearLayout>
</layout>

很直觀,我們可以看到四個(gè)標(biāo)簽<layout><data><import><variable>


  • <layout>
    1.其實(shí)在DataBinding中,它必須是最外層的標(biāo)簽,是根布局,里面有且只能包裹一個(gè)子View,就像Scrollview。
    2.xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    這兩個(gè)命名控件一定要加上
    3.layout標(biāo)簽的直接子標(biāo)簽不能是merge,否則報(bào)錯(cuò)。
    4.fragment標(biāo)簽不支持dataBinding表達(dá)式,即在fragment標(biāo)簽中使用任何dataBinding表達(dá)式都會(huì)報(bào)錯(cuò)
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="data"
            type="String"></variable>
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <fragment
            android:layout_width="match_parent"
            android:tag="@{data}"
            android:layout_height="match_parent"></fragment>
    </LinearLayout>
</layout>

查看源碼就可知道原因

else if ("fragment".equals(nodeName)) {
    if (XmlEditor.hasExpressionAttributes(parent)) {
        L.e("fragments do not support data binding expressions.");
    }
    continue;
}
  • <data>
    1.<data>這個(gè)標(biāo)簽,其實(shí)就是用來(lái)承載數(shù)據(jù)的,在其內(nèi)部可以定義多個(gè)<import><variable>標(biāo)簽
    2.<data>標(biāo)簽,有且只能定義一個(gè)
    3.<data>有個(gè)class數(shù)據(jù),就是用來(lái)定義,編譯出的binding的類的位置
    下圖就是我未定義class,默認(rèn)的路徑,可以看出有四個(gè)布局文件,使用了DataBinding


    深度截圖20170916173117.png
  • <import>

<import
            type="com.example.scs.myapplication.StudentBean"
            alias="student1">
</import>
<import
            type="com.example.scs.myapplication.StudentBean"
            alias="student2">
</import>

<import>有兩個(gè)屬性,type就是要引用類的位置,alias就是別名。
別名的用途如上所示,當(dāng)定義了多個(gè)同type的import的時(shí)候,就是要用別名去區(qū)分,不然就要出大事情

  • <variable>
<variable
            name="data"
            type="String">
</variable>
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:text="@{data}"
            android:layout_height="match_parent" />
    </LinearLayout>

<variable>標(biāo)簽可謂是DataBinding機(jī)制的核心之一,是java代碼和xml的樞紐??梢岳斫鉃橐壎ǖ淖兞?,name就是變量的名字,type就是變量的類型。
如上述代碼所示,TextView就綁定了data數(shù)據(jù)。

好了 基本的概念的陳述完了,欲知詳情請(qǐng)看DataBinding(2)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容