所謂Data Binding(以下簡稱DB),就是把數(shù)據(jù)提供者和消費(fèi)者綁定起來并實(shí)時同步。DB其實(shí)并不是安卓所獨(dú)有的,不過在安卓領(lǐng)域,DB一般指谷歌提供的DB框架,作用就是把數(shù)據(jù)和UI綁定同步。
在DB出現(xiàn)以前,數(shù)據(jù)和UI的同步都是靠手動實(shí)現(xiàn)。現(xiàn)在,DB能夠(部分)替我們完成這項(xiàng)工作。
使用準(zhǔn)備
只需在app級的build.gradle
文件內(nèi)添加以下聲明。當(dāng)然,假如有別的模塊使用DB,也要做類似聲明。
android {
....
dataBinding {
enabled = true
}
}
據(jù)說以前還要添加第三方庫依賴,以及對Android Studio的版本有一定要求。具體可以參考官網(wǎng)。
Layout文件的改變
DB要求對layout的XML文件做一定變化,即以<layout ...>
元素開頭,之后跟一個<data...>
元素。官網(wǎng)給的例子如下:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.example.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.lastName}"/>
</LinearLayout>
</layout>
大致可以理解為<data...>
元素里聲明的是依賴的數(shù)據(jù)。聲明過的數(shù)據(jù)在下面就可以使用@{}
符號來表示綁定。
當(dāng)然,這里還支持一系列的操作符,具體可以參考官網(wǎng)。不過我個人不是很支持在XML文件里面帶入過于復(fù)雜的邏輯代碼,因?yàn)檎{(diào)試可能會變得困難,而且編譯器對XML的邏輯代碼語法支持也不是很完美。
好了,我也不打算把官網(wǎng)文章翻譯一遍,還是自己動手搞一個例子吧。
第一個DB項(xiàng)目
使用DB實(shí)現(xiàn)一個更改用戶名的項(xiàng)目,要求能實(shí)現(xiàn)實(shí)時同步。
首先看看數(shù)據(jù)層。DB在這里提供了三種選擇,我們需要學(xué)習(xí)一下來決定采用哪一種:
- Observable Objects。這是對POJO( plain old Java object)對象的綁定,具體實(shí)現(xiàn)就是在get方法上面添加標(biāo)注
@Bindable
。 - ObservableFields。這是對某個成員的綁定,具體做法就是用
public final
修飾的Observable<T>
來包裹成員變量,然后修改和讀取方法也有變化,需要使用set和get方法。 - Observable Collections。顧名思義,這是對集合的綁定,例如
ObservableArrayList<T>
和ObservableArrayMap<K, V>
。
不是很理解谷歌的排序,不過今天就用ObservableFields來實(shí)現(xiàn)。
數(shù)據(jù)對象
public class User {
public final ObservableField<String> name = new ObservableField<String>() {};
}
官網(wǎng)教程上是static對象內(nèi)定義的,事實(shí)證明非static的對象內(nèi)也可以。
Layout文件
我們需要一個EditText
控件來輸入,然后一個Button
來顯示數(shù)據(jù)的值,方便知道數(shù)據(jù)是否真的改變了。
用你自己的包名替換your.package.name。
<?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>
<variable name="user" type="your.package.name.User"/>
<variable name="activity" type="your.package.name.MainActivity"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="your.package.name.MainActivity">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/name"
android:text="@={user.name}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/editText"
android:inputType="textCapWords"
android:layout_marginLeft="16dp"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
<Button
android:onClick="onClick"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/editText"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
</layout>
注意我使用了android:text="@={user.name}
,而不是android:text="@{user.name}
,因?yàn)楹笳咧皇菃蜗蚪壎ā9倬W(wǎng)關(guān)于這一點(diǎn)絲毫未提及,有點(diǎn)坑的。
當(dāng)然,要用android:text="@{user.name}
也行,不過值的反饋需要自己再寫一個@BindingAdapter
注解函數(shù),多少有點(diǎn)麻煩。
另外,我這里Button
直接引用MainActivity
內(nèi)的onClick(View v)
函數(shù),這個跟DB關(guān)系不大。假如不是MainActivity
而是別的什么的話,又是另一種做法了,以后再說。
綁定
MainActivity
public class MainActivity extends AppCompatActivity {
private User user = new User();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
user.name.set("Mark");
binding.setUser(user);
}
public void onClick(View v) {
Toast.makeText(MainActivity.this, user.name.get(), Toast.LENGTH_SHORT).show();
}
}
綁定其實(shí)不難,根據(jù)Layout文件的名字,對應(yīng)的Binding
對象會在名字后面加上Binding。好了,更改名字再按Button
可以發(fā)現(xiàn)確實(shí)實(shí)現(xiàn)了雙向綁定。
總結(jié)與回顧
- DB的準(zhǔn)備工作
- Layout文件
- Layout依賴的聲明
- Layout數(shù)據(jù)的綁定
- 數(shù)據(jù)層的綁定方式
- ObservableField
- ObservableCollections
- ObservableObjects
- 綁定細(xì)節(jié)