Databinding概述:
數(shù)據(jù)綁定庫(kù)是一種支持庫(kù),借助該庫(kù),您可以使用聲明性格式(而非程序化地)將布局中的界面組件綁定到應(yīng)用中的數(shù)據(jù)源。
布局通常是使用調(diào)用界面框架方法的代碼在 Activity 中定義的。例如,以下代碼調(diào)用 findViewById() 來(lái)查找 TextView 微件并將其綁定到 viewModel 變量的 userName 屬性:
? ? findViewById<TextView>(R.id.sample_text).apply {
text
= viewModel.userName
}
以下示例展示了如何在布局文件中使用數(shù)據(jù)綁定庫(kù)將文本直接分配到微件。這樣就無(wú)需調(diào)用上述任何 Java 代碼。請(qǐng)注意賦值表達(dá)式中 @{} 語(yǔ)法的使用:
<TextViewandroid:text="@{viewmodel.userName}" />
借助布局文件中的綁定組件,您可以移除 Activity 中的許多界面框架調(diào)用,使其維護(hù)起來(lái)更簡(jiǎn)單、方便。還可以提高應(yīng)用性能,并且有助于防止內(nèi)存泄漏以及避免發(fā)生 Null 指針異常。
使用數(shù)據(jù)綁定庫(kù)
要了解如何在 Android 應(yīng)用中使用數(shù)據(jù)綁定庫(kù),請(qǐng)參閱以下頁(yè)面。
了解如何準(zhǔn)備開(kāi)發(fā)環(huán)境以使用數(shù)據(jù)綁定庫(kù),包括在 Android Studio 中支持?jǐn)?shù)據(jù)綁定代碼。
借助表達(dá)式語(yǔ)言,您可以編寫(xiě)將變量關(guān)聯(lián)到布局中的視圖的表達(dá)式。數(shù)據(jù)綁定庫(kù)會(huì)自動(dòng)生成將布局中的視圖與您的數(shù)據(jù)對(duì)象綁定所需的類(lèi)。該庫(kù)提供了可在布局中使用的導(dǎo)入、變量和包含等功能。
該庫(kù)的這些功能可與您的現(xiàn)有布局無(wú)縫地共存。例如,可以在表達(dá)式中使用的綁定變量在 data 元素(界面布局根元素的同級(jí))內(nèi)定義。這兩個(gè)元素都封裝在 layout 標(biāo)記中,如以下示例所示:
<layout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><data><variablename="viewmodel"type="com.myapp.data.ViewModel" /></data><ConstraintLayout... /> <!-- UI layout's root element --></layout>
數(shù)據(jù)綁定庫(kù)提供了可讓您輕松地觀察數(shù)據(jù)更改情況的類(lèi)和方法。您不必操心在底層數(shù)據(jù)源發(fā)生更改時(shí)刷新界面。您可以將變量或其屬性設(shè)為可觀察。借助該庫(kù),您可以將對(duì)象、字段或集合設(shè)為可觀察。
數(shù)據(jù)綁定庫(kù)可以生成用于訪問(wèn)布局變量和視圖的綁定類(lèi)。此頁(yè)面展示了如何使用和自定義所生成的綁定類(lèi)。
每一個(gè)布局表達(dá)式都有一個(gè)對(duì)應(yīng)的綁定適配器,要求必須進(jìn)行框架調(diào)用來(lái)設(shè)置相應(yīng)的屬性或監(jiān)聽(tīng)器。例如,綁定適配器負(fù)責(zé)調(diào)用 setText() 方法來(lái)設(shè)置文本屬性,或者調(diào)用 setOnClickListener() 方法向點(diǎn)擊事件添加監(jiān)聽(tīng)器。最常擁的綁定適配器(例如針對(duì)本頁(yè)面的示例中使用的 android:text 屬性)可供您在 android.databinding.adapters 軟件包中使用。如需常用綁定適配器列表,請(qǐng)參閱適配器。您也可以按照以下示例所示創(chuàng)建自定義適配器:
? ? @BindingAdapter("app:goneUnless")fun goneUnless(view: View, visible: Boolean) {
view
.visibility = if (visible) View.VISIBLE else View.GONE
}
Android 支持庫(kù)包含架構(gòu)組件,您可以使用這些組件設(shè)計(jì)穩(wěn)健、可測(cè)試且易維護(hù)的應(yīng)用。您可以將架構(gòu)組件與數(shù)據(jù)綁定庫(kù)一起使用,以進(jìn)一步簡(jiǎn)化界面開(kāi)發(fā)。
數(shù)據(jù)綁定庫(kù)支持雙向數(shù)據(jù)綁定。此類(lèi)綁定使用的表示法支持以下操作:接收對(duì)屬性的數(shù)據(jù)更改,同時(shí)監(jiān)聽(tīng)用戶對(duì)此屬性的更新。
當(dāng)然以上都是在谷歌開(kāi)發(fā)者官方網(wǎng)站都可以找到,需要的同學(xué)可以到官網(wǎng)查看谷歌官方傳送門(mén)
下面就開(kāi)始我門(mén)今天的示例操作:
首先我門(mén)的前期工作就是創(chuàng)建相應(yīng)的activity和viewModel以及布局文件,當(dāng)然這個(gè)和前面的將Livedata時(shí)候的文件基本相同,這里就不在做過(guò)多的說(shuō)明了,想要的同學(xué)可以看前面的文章,或者在文章結(jié)束的時(shí)候,我會(huì)給大家我的項(xiàng)目示例的GitHub地址,需要的同學(xué)可以下載。
首先來(lái)的我先將databinding在我門(mén)的項(xiàng)目中啟用,這很簡(jiǎn)單,只需要在我門(mén)的gradle中添加以下代碼就可以了
dataBinding{
? ? enabled=true
}
設(shè)置完成之后,我們同步gradle就可以使用dataBinding了。
首先來(lái)到布局文件,在布局文件中的最上面的有一個(gè)小黃色的燈泡,點(diǎn)擊燈泡,就可以看到一個(gè)轉(zhuǎn)換為databinding布局文件的提示;
轉(zhuǎn)換完成之后的布局文件樣式如下:
就會(huì)發(fā)現(xiàn)根布局文件變成了layout了,并多了一個(gè)data包裹的區(qū)域。
完成布局文件的轉(zhuǎn)換之后,來(lái)到activity中進(jìn)行dataBinding的綁定。綁定如下
public class DataBindActivity extends AppCompatActivity {
private DataBindingViewModel dataBindingViewModel;
//設(shè)置dataBinding
private ActivityDataBindBinding dataBindBinding;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//綁定不布局文件
dataBindBinding=DataBindingUtil.setContentView(this,R.layout.activity_data_bind);
? ? ? ? dataBindingViewModel=new ViewModelProvider(this).get(DataBindingViewModel.class);
//綁定viewModel數(shù)據(jù)層,setData跟布局文件中的name=data相同
? ? dataBindBinding.setData(dataBindingViewModel);
//生命周期監(jiān)控
dataBindBinding.setLifecycleOwner(this);
? ? }
}
經(jīng)過(guò)綁定之后我門(mén)能夠發(fā)現(xiàn),比起之前activity中的代碼量少了很多,看起來(lái)更加的舒服了。
然后我到布局文件中進(jìn)行數(shù)據(jù)和點(diǎn)擊事件的綁定代碼如下:
<?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">
? ? ? ? name="data"
? ? ? ? type="com.example.jetpackdemo.dataBinding.DataBindingViewModel" />
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? tools:context=".dataBinding.DataBindActivity">
? ? ? ? ? ? android:id="@+id/guideline"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:orientation="vertical"
? ? ? ? ? ? app:layout_constraintGuide_begin="205dp" />
? ? ? ? ? ? android:id="@+id/btn1"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="按鈕1"
? ? ? ? ? ? android:onClick="@{()->data.addTickadd1()}"
? ? ? ? ? ? app:layout_constraintBottom_toBottomOf="parent"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent" />
? ? ? ? ? ? android:id="@+id/btn2"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="按鈕2"
? ? ? ? ? ? android:onClick="@{()->data.addTickadd2()}"
? ? ? ? ? ? app:layout_constraintBottom_toBottomOf="parent"
? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent" />
? ? ? ? ? ? android:id="@+id/tv1"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="@{String.valueOf(data.tickadd1)}"
? ? ? ? ? ? app:layout_constraintBottom_toTopOf="@+id/btn1"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent" />
? ? ? ? ? ? android:id="@+id/tv2"
? ? ? ? ? ? android:layout_width="47dp"
? ? ? ? ? ? android:layout_height="19dp"
? ? ? ? ? ? android:text="@{String.valueOf(data.tickadd2)}"
? ? ? ? ? ? app:layout_constraintBottom_toTopOf="@+id/btn2"
? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent" />
</layout>
到這里基本就完成了數(shù)據(jù)的雙向綁定了,接下來(lái)看看效果吧!
可以看到實(shí)現(xiàn)了我們的目的。
下面是本項(xiàng)目的GitHub地址