事件綁定
Method References
事件可以被直接綁定到handler迫横,類似于Activity中onClick可以被指派給一個(gè)方法。相比View#onClick的一個(gè)優(yōu)勢(shì)是贫橙,這個(gè)表達(dá)式會(huì)在編譯時(shí)被處理,所以如果方法不存在或者簽名不對(duì)反粥,或編譯錯(cuò)誤卢肃。
Method References和Listener Bindings的一個(gè)主要區(qū)別是,前者的listener的真正實(shí)現(xiàn)是在data被綁定的時(shí)候才顿,而不是事件被觸發(fā)的時(shí)候莫湘。如果想在事件發(fā)生時(shí)評(píng)估表達(dá)式,請(qǐng)用listener binding娜膘。
分配event給handler逊脯,使用一個(gè)正常的binding表達(dá)式,它的值就是被調(diào)用的方法名竣贪。例如,if your data object has two methods:
public class MyHandlers {
public void onClickFriend(View view) { ... }
}
binding表達(dá)式可以分配click listener到一個(gè)View:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="handlers" type="com.example.Handlers"/>
<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}"
android:onClick="@{handlers::onClickFriend}"/>
</LinearLayout>
</layout>
注意军洼,表達(dá)式(expression)中的方法的簽名(signature)必須和Listener Object中的方法的簽名完全一致。
Listener Bindings
Listener Bindings使用更為靈活演怎,跟Method References類似匕争,但可以讓你運(yùn)行隨意的data binding表達(dá)式碟联。這里不介紹了右核。
Layout的細(xì)節(jié)
1. Imports
在data節(jié)點(diǎn)中這樣添加引用:
<data>
<import type="android.view.View"/>
</data>
這樣,你可以在binding表達(dá)式中使用View了:
<TextView
android:text="@{user.lastName}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/>
類名沖突的時(shí)候撵割,可以取別名(alias):
<import type="android.view.View"/>
<import type="com.example.real.estate.View"
alias="Vista"/>
variables和表達(dá)式的type references中都可以用引用的type:
<data>
<import type="com.example.User"/>
<import type="java.util.List"/>
<variable name="user" type="User"/>
<variable name="userList" type="List<User>"/>
</data>
在表達(dá)式中引用靜態(tài)field和方法的時(shí)候也能使用引用的type:
<data>
<import type="com.example.MyStringUtils"/>
<variable name="user" type="com.example.User"/>
</data>
…
<TextView
android:text="@{MyStringUtils.capitalize(user.lastName)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
跟Java中一樣歹叮,java.lang.*這種類會(huì)被自動(dòng)導(dǎo)入跑杭。
2. Variables
這里涉及到實(shí)現(xiàn)Observable或observable collection的問(wèn)題,暫時(shí)調(diào)過(guò)咆耿。
3. 自定義Binding類名稱
默認(rèn)情況下會(huì)根據(jù)layout文件生成一個(gè)Binding類德谅,放置在包名根目錄下。
也可以通過(guò)在data節(jié)點(diǎn)加入路徑和Binding類名來(lái)自定義:
<data class="com.example.ContactItem">
...
</data>
4. Includes
Variables may be passed into an included layout's binding from the containing layout by using the application namespace and the variable name in an attribute:
通過(guò)使用application namespace和variable name萨螺,可以把變量傳遞給一個(gè)被包含的layout里面窄做。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="user" type="com.example.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/name"
bind:user="@{user}"/>
<include layout="@layout/contact"
bind:user="@{user}"/>
</LinearLayout>
</layout>
這里,name.xml和contact.xml里面必須要有user variable慰技。
Data Binding不支持include merge 元素的直接child椭盏,下面這樣是不行的:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="user" type="com.example.User"/>
</data>
<merge>
<include layout="@layout/name"
bind:user="@{user}"/>
<include layout="@layout/contact"
bind:user="@{user}"/>
</merge>
</layout>
5. 表達(dá)式
通用特性:
- Mathematical + - / * %
- String concatenation +
- Logical && ||
- Binary & | ^
- Unary + - ! ~
- Shift >> >>> <<(移位 )
- Comparison == > < >= <=
- instanceof
- Grouping ()
- Literals - character, String, numeric, null
- Cast
- Method calls(方法調(diào)用)
- Field access
- Array access []
- Ternary operator(三元運(yùn)算) ?:
e.g.
android:text="@{String.valueOf(index + 1)}"
android:visibility="@{age < 13 ? View.GONE : View.VISIBLE}"
android:transitionName='@{"image_" + id}'
缺少的:
有些Java中能用的操作,在表達(dá)式函數(shù)中不能用:
- this
- super
- new
- Explicit generic invocatio
Null Coalescing 操作:
如果是null吻商,選左邊掏颊;不是,選右邊
android:text="@{user.displayName ?? user.lastName}"
等價(jià)于:
android:text="@{user.displayName != null ? user.displayName : user.lastName}"
Property Reference:
使用這樣的格式引用類的properties艾帐、fields乌叶、getters和ObservableFields:
android:text="@{user.lastName}"
避免 NullPointerException
Data Binding代碼生成時(shí)會(huì)自動(dòng)檢查null改化,如果存在null,會(huì)為對(duì)象賦默認(rèn)值枉昏。
集合
常見(jiàn)的有 arrays, lists, sparse lists, and maps;為了簡(jiǎn)便揍鸟,可以使用[]來(lái)調(diào)用兄裂。
<data>
<import type="android.util.SparseArray"/>
<import type="java.util.Map"/>
<import type="java.util.List"/>
<variable name="list" type="List<String>"/>
<variable name="sparse" type="SparseArray<String>"/>
<variable name="map" type="Map<String, String>"/>
<variable name="index" type="int"/>
<variable name="key" type="String"/>
</data>
…
android:text="@{list[index]}"
…
android:text="@{sparse[index]}"
…
android:text="@{map[key]}"
字符串
這樣用:
android:text='@{map["firstName"]}'
或者這樣:
android:text="@{map[`firstName`}"
android:text="@{map['firstName']}"
資源
簡(jiǎn)單的例子:
android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}"
6. Data 對(duì)象
任何POJO都可用于Data Binding。修改POJO不會(huì)導(dǎo)致UI更新阳藻。data binding真正的力量在于晰奖,讓你的data objects對(duì)data changes有notify的能力。有三種數(shù)據(jù)變化通知機(jī)制:
- observable objects
- observable fields
- observable collections
更多請(qǐng)看:https://developer.android.com/topic/libraries/data-binding/index.html#listener_bindings