Databinding概述:
數(shù)據(jù)綁定庫是一種支持庫找默,借助該庫,您可以使用聲明性格式(而非程序化地)將布局中的界面組件綁定到應(yīng)用中的數(shù)據(jù)源吼驶。
布局通常是使用調(diào)用界面框架方法的代碼在 Activity 中定義的惩激。例如,以下代碼調(diào)用 findViewById() 來查找 TextView 微件并將其綁定到 viewModel 變量的 userName 屬性:
? ? findViewById<TextView>(R.id.sample_text).apply {
text
= viewModel.userName
}
以下示例展示了如何在布局文件中使用數(shù)據(jù)綁定庫將文本直接分配到微件蟹演。這樣就無需調(diào)用上述任何 Java 代碼风钻。請注意賦值表達(dá)式中 @{} 語法的使用:
<TextViewandroid:text="@{viewmodel.userName}" />
借助布局文件中的綁定組件,您可以移除 Activity 中的許多界面框架調(diào)用酒请,使其維護(hù)起來更簡單骡技、方便。還可以提高應(yīng)用性能羞反,并且有助于防止內(nèi)存泄漏以及避免發(fā)生 Null 指針異常布朦。
使用數(shù)據(jù)綁定庫
要了解如何在 Android 應(yīng)用中使用數(shù)據(jù)綁定庫,請參閱以下頁面昼窗。
了解如何準(zhǔn)備開發(fā)環(huán)境以使用數(shù)據(jù)綁定庫是趴,包括在 Android Studio 中支持?jǐn)?shù)據(jù)綁定代碼。
借助表達(dá)式語言澄惊,您可以編寫將變量關(guān)聯(lián)到布局中的視圖的表達(dá)式唆途。數(shù)據(jù)綁定庫會自動生成將布局中的視圖與您的數(shù)據(jù)對象綁定所需的類。該庫提供了可在布局中使用的導(dǎo)入掸驱、變量和包含等功能窘哈。
該庫的這些功能可與您的現(xiàn)有布局無縫地共存。例如亭敢,可以在表達(dá)式中使用的綁定變量在 data 元素(界面布局根元素的同級)內(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ù)綁定庫提供了可讓您輕松地觀察數(shù)據(jù)更改情況的類和方法帅刀。您不必操心在底層數(shù)據(jù)源發(fā)生更改時(shí)刷新界面让腹。您可以將變量或其屬性設(shè)為可觀察。借助該庫扣溺,您可以將對象骇窍、字段或集合設(shè)為可觀察。
數(shù)據(jù)綁定庫可以生成用于訪問布局變量和視圖的綁定類锥余。此頁面展示了如何使用和自定義所生成的綁定類腹纳。
每一個(gè)布局表達(dá)式都有一個(gè)對應(yīng)的綁定適配器,要求必須進(jìn)行框架調(diào)用來設(shè)置相應(yīng)的屬性或監(jiān)聽器驱犹。例如嘲恍,綁定適配器負(fù)責(zé)調(diào)用 setText() 方法來設(shè)置文本屬性,或者調(diào)用 setOnClickListener() 方法向點(diǎn)擊事件添加監(jiān)聽器雄驹。最常擁的綁定適配器(例如針對本頁面的示例中使用的 android:text 屬性)可供您在 android.databinding.adapters 軟件包中使用佃牛。如需常用綁定適配器列表,請參閱適配器医舆。您也可以按照以下示例所示創(chuàng)建自定義適配器:
? ? @BindingAdapter("app:goneUnless")fun goneUnless(view: View, visible: Boolean) {
view
.visibility = if (visible) View.VISIBLE else View.GONE
}
Android 支持庫包含架構(gòu)組件俘侠,您可以使用這些組件設(shè)計(jì)穩(wěn)健、可測試且易維護(hù)的應(yīng)用蔬将。您可以將架構(gòu)組件與數(shù)據(jù)綁定庫一起使用爷速,以進(jìn)一步簡化界面開發(fā)。
數(shù)據(jù)綁定庫支持雙向數(shù)據(jù)綁定霞怀。此類綁定使用的表示法支持以下操作:接收對屬性的數(shù)據(jù)更改惫东,同時(shí)監(jiān)聽用戶對此屬性的更新。
當(dāng)然以上都是在谷歌開發(fā)者官方網(wǎng)站都可以找到里烦,需要的同學(xué)可以到官網(wǎng)查看谷歌官方傳送門
下面就開始我門今天的示例操作:
首先我門的前期工作就是創(chuàng)建相應(yīng)的activity和viewModel以及布局文件凿蒜,當(dāng)然這個(gè)和前面的將Livedata時(shí)候的文件基本相同,這里就不在做過多的說明了胁黑,想要的同學(xué)可以看前面的文章废封,或者在文章結(jié)束的時(shí)候,我會給大家我的項(xiàng)目示例的GitHub地址丧蘸,需要的同學(xué)可以下載漂洋。
首先來的我先將databinding在我門的項(xiàng)目中啟用,這很簡單力喷,只需要在我門的gradle中添加以下代碼就可以了
dataBinding{
? ? enabled=true
}
設(shè)置完成之后刽漂,我們同步gradle就可以使用dataBinding了。
首先來到布局文件弟孟,在布局文件中的最上面的有一個(gè)小黃色的燈泡贝咙,點(diǎn)擊燈泡,就可以看到一個(gè)轉(zhuǎn)換為databinding布局文件的提示拂募;
轉(zhuǎn)換完成之后的布局文件樣式如下:
就會發(fā)現(xiàn)根布局文件變成了layout了,并多了一個(gè)data包裹的區(qū)域。
完成布局文件的轉(zhuǎn)換之后才避,來到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)過綁定之后我門能夠發(fā)現(xiàn),比起之前activity中的代碼量少了很多,看起來更加的舒服了。
然后我到布局文件中進(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ù)的雙向綁定了吊说,接下來看看效果吧!
可以看到實(shí)現(xiàn)了我們的目的优炬。
下面是本項(xiàng)目的GitHub地址