Data Binding允許通過寫表達(dá)式來(lái)處理view分發(fā)的事件霎箍,e.g.onClick。事件屬性名通常通過listener方法的名字管理漂坏,但存在個(gè)別例外缀壤。e.g.View.OnLongClickListener有個(gè)方法onLongClick()纠亚,這時(shí)事件的屬性名是android:onLongClick筋夏。
有兩種方法來(lái)處理事件
- 方法引用Method Reference
在表達(dá)式中,可以引用符合監(jiān)聽器中方法簽名的方法骗随。 - 監(jiān)聽器綁定Listener Bindings
使用lambda表達(dá)式,當(dāng)事件發(fā)生時(shí)鸿染,對(duì)這些lambda表達(dá)式進(jìn)行計(jì)算乞巧。
方法引用
官方說明:In your expressions, you can reference methods that conform to the signature of the listener method. When an expression evaluates to a method reference, Data Binding wraps the method reference and owner object in a listener, and sets that listener on the target view. If the expression evaluates to null, Data Binding does not create a listener and sets a null listener instead.
事件和處理方法可被直接綁定,這和android:onClick能被Activity中方法賦值類似绽媒。方法引用相比View#onClick屬性的主要優(yōu)點(diǎn)是表達(dá)式在編譯時(shí)處理,如果方法不存在或者簽名不對(duì)囤热,你將會(huì)收到一個(gè)編譯錯(cuò)誤获三。
方法引用和監(jiān)聽器綁定之間的主要區(qū)別是前者在數(shù)據(jù)綁定時(shí)創(chuàng)建真正的監(jiān)聽器實(shí)現(xiàn),而不是事件被觸發(fā)時(shí)疙教。如果你傾向與事件發(fā)生時(shí)計(jì)算表達(dá)式,請(qǐng)使用監(jiān)聽器綁定方法躺屁。
To assign an event to its handler, use a normal binding expression, with the value being the method name to call. For example, if your data object has two methods:
事件處理方法示例:
public class MyHandlers {
public void onClickFriend(View view) { ... }
}
布局文件中對(duì)點(diǎn)擊事件監(jiān)聽器賦值的表達(dá)式如下:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="handlers" type="com.example.MyHandlers"/>
<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á)式中方法的簽名必須和監(jiān)聽器對(duì)象中的方法簽名完全保持一致经宏。
在Activity文件中創(chuàng)建綁定關(guān)系,可以看到需要設(shè)置布局文件中定義的兩個(gè)variable:user和handlers
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ActivityMain2Binding binding = DataBindingUtil.setContentView(this, R.layout.activity_main2);
User user = new User("Test", "User");
binding.setUser(user);
binding.setHandlers(new MyHandlers());
}
監(jiān)聽器綁定
官方說明:These are lambda expressions that are evaluated when the event happens. Data Binding always creates a listener, which it sets on the view. When the event is dispatched, the listener evaluates the lambda expression.
Listener Bindings are binding expressions that run when an event happens. They are similar to method references, but they let you run arbitrary data binding expressions. This feature is available with Android Gradle Plugin for Gradle version 2.0 and later.
在方法引用事件處理中耐亏,方法的參數(shù)必須和事件監(jiān)聽器的一致沪斟。而在監(jiān)聽器綁定事件處理中暇矫,只要求返回值和監(jiān)聽器的保持一致择吊,除非監(jiān)聽器返回空李根。示例:
public class Presenter {
public void onSaveClick(Task task){}
}
綁定點(diǎn)擊事件方法示例:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="task" type="com.android.example.Task" />
<variable name="presenter" type="com.android.example.Presenter" />
</data>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="@{() -> presenter.onSaveClick(task)}" />
</LinearLayout>
</layout>
監(jiān)聽器使用lambda表達(dá)式房轿,只能作為表達(dá)式的根元素所森。當(dāng)在表達(dá)式中使用回調(diào)時(shí)囱持,Data Binding自動(dòng)創(chuàng)建必要的監(jiān)聽器并注冊(cè)事件焕济。當(dāng)view觸發(fā)事件時(shí),Data Binding對(duì)表達(dá)式進(jìn)行求值掩幢。
As in regular binding expressions, you still get the null and thread safety of Data Binding while these listener expressions are being evaluated.
上例中并未定義點(diǎn)擊onClick(android.view.View)中傳入的view參數(shù)上鞠。關(guān)于監(jiān)聽器參數(shù),有兩種選擇:一忽略所有的參數(shù)旗国;二命名所有參數(shù)。如果選擇命名這些參數(shù)能曾,則可以在表達(dá)式中使用。上述表達(dá)式也可以寫為:
android:onClick="@{(view) -> presenter.onSaveClick(task)}"
如果想在表達(dá)式中使用參數(shù)蕊程,方法如下:
public class Presenter {
public void onSaveClick(View view, Task task){}
}
android:onClick="@{(theView) -> presenter.onSaveClick(theView, task)}"
lamda表達(dá)式可以有多個(gè)參數(shù)驼唱。
public class Presenter {
public void onCompletedChanged(Task task, boolean completed){}
}
//監(jiān)聽事件原型 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
<CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onCheckedChanged="@{(cb, isChecked) -> presenter.completeChanged(task, isChecked)}" />
如果監(jiān)聽的事件返回值不是空,則表達(dá)式中需要返回一樣的類型玫恳。例如監(jiān)聽長(zhǎng)按事件時(shí),需要返回false掀序。
public class Presenter {
public boolean onLongClick(View view, Task task){}
}
//監(jiān)聽事件原型 boolean onLongClick(View var1)
android:onLongClick="@{(theView) -> presenter.onLongClick(theView, task)}"
如果因?yàn)閚ull對(duì)象導(dǎo)致表達(dá)式不能求值惭婿,Data Binding返回java中該類型的默認(rèn)值不恭。e.g. 引用類型返回null,int返回0折晦,boolean返回false沾瓦,etc.
如果需要使用表達(dá)式做判斷(如三元組)满着,void可以用來(lái)作為一個(gè)符號(hào)暴拄。
android:onClick="@{(v) -> v.isVisible() ? doSomething() : void}"
避免使用復(fù)雜的監(jiān)聽器
監(jiān)聽器表達(dá)式功能很強(qiáng)大编饺,并讓代碼更易讀。但是撕蔼,監(jiān)聽器如果使用復(fù)雜的表達(dá)式會(huì)使得布局文件很難閱讀和難以維護(hù)。這些表達(dá)式應(yīng)該盡量簡(jiǎn)單鲸沮,只承擔(dān)將可用數(shù)據(jù)從UI傳遞到回調(diào)函數(shù)的作用锅论。任何商業(yè)邏輯的實(shí)現(xiàn),都應(yīng)該放在表達(dá)式中觸發(fā)的回調(diào)函數(shù)中最易。
參考文檔:https://developer.android.google.cn/topic/libraries/data-binding/index.html#build_environment