通過(guò)DataBinding,事件的 處理變得更加簡(jiǎn)潔,不單單是支持 點(diǎn)擊,同時(shí)也支持一些其他事件。例如:CheckBox的 onCheckedChange,TextView的onTextChanged,同時(shí)也支持借助一些表達(dá)式指定事件的處理者。
1、普通事件
<!--在res 文件的使用如下 ,當(dāng)然你需要在 layout 布局中聲明-->
<!-- DataBinding 是可以支持泛型的 -->
<layout>
<data>
<variable
name="holder"
type="com.zk.sample.module.binding.view.DataBindingFragment.DataBindingFace" />
</data>
<CheckBox
android:id="@+id/cb_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:checked="true"
android:onCheckedChangeListener="@{holder.onCheckedChangeListener}"
android:text="事件" />
</layout>
holder 也就是ViewHolder,主要是處理一些用戶(hù)操作后的數(shù)據(jù)處理及相應(yīng)結(jié)果的再次展示,實(shí)現(xiàn)如下
//單獨(dú)的事件處理
public class DataBingHolder implements DataBindingFragment.DataBindingFace {
public static DataBingHolder holder;
private DataBingHolder() {
}
public static DataBingHolder getInstance() {
if (holder == null) {
synchronized (DataBingHolder.class) {
if (holder == null) {
holder = new DataBingHolder();
}
}
}
return holder;
}
/**
* checkBox 的狀態(tài)監(jiān)聽(tīng)
*
*/
public CompoundButton.OnCheckedChangeListener getOnCheckedChangeListener() {
return new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
FragmentDataBindingBinding binding = DataBindingUtil.findBinding(buttonView);
switch (buttonView.getId()) {
case R.id.cb_event:
//選中時(shí)顯示事件列表
binding.llEvent.setVisibility(isChecked ? View.VISIBLE : View.GONE);
break;
}
}
};
}
}
// View 中事件的調(diào)用
//此處使用單例模式保證數(shù)據(jù)源的唯一性
DataBingHolder holder = DataBingHolder.getInstance();
binding.setHolder(holder);
2、自定義事件
DataBinding 支持自定義事件,可以擴(kuò)展事件參數(shù),并指定是否是必須參數(shù)
1、聲明自定義事件 必須是靜態(tài)的
public class BindUtil {
private static final String TAG = "BindUtil";
/**
* requireAll 默認(rèn)為true 如果不設(shè)置一個(gè)控件中xml中三個(gè)必須同時(shí)具備才可以正常編譯
*/
// @BindingAdapter("imageUrl")//單個(gè)參數(shù)
// @BindingAdapter({"imageUrl", "error", "placeHolder"})//多個(gè)參數(shù)
@BindingAdapter(value = {"imageUrl", "error", "placeHolder"}, requireAll = false)//非必須
public static void loadImage(ImageView view, String url, Drawable error, Drawable placeHolder) {
LogUtil.d(TAG, "load image ->url:" + url);
DrawableTypeRequest<String> request = Glide.with(view.getContext()).load(url);
if (error != null) {
request.error(error);
}
if (placeHolder != null) {
request.placeholder(placeHolder);
}
request.into(view);
}
}
2、xml 文件中的使用 (當(dāng)參數(shù)默認(rèn)需要全部時(shí)必須要寫(xiě)全,負(fù)責(zé)會(huì)導(dǎo)致變異失?。?/p>
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="holder"
type="com.zk.sample.module.binding.view.DataBindingFragment.DataBindingFace" />
<!-- 聲明圖片的數(shù)據(jù)源 -->
<variable
name="imageUrl"
type="String" />
<variable
name="event"
type="com.zk.sample.module.binding.BindingEvent" />
</data>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey"
android:orientation="vertical">
<CheckBox
android:id="@+id/cb_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:checked="true"
android:onCheckedChangeListener="@{holder.onCheckedChangeListener}"
android:text="事件" />
<LinearLayout
android:id="@+id/ll_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<!--事件處理者的指定 及自定義傳參-->
<Button
style="@style/ButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{(theview)->event.getDataOnClick(theview,imageUrl)}"
android:text="傳參事件(獲取圖片網(wǎng)址)" />
<!--自定義事件的觸發(fā)控件 通過(guò)點(diǎn)擊觸發(fā)-->
<Button
style="@style/ButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{holder.imageOnClickListener}"
android:text="自定義事件(加載圖片)" />
<!-- 自定義事件所需參數(shù) 及結(jié)果顯示-->
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitCenter"
app:error="@{@color/black}"
app:imageUrl="@{imageUrl}"
app:placeHolder="@{@color/holderColor}" />
<!--事件的可以直接傳送數(shù)據(jù)源 -->
<Button
style="@style/ButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{()->event.setParamsOnClick(userImg)}"
android:text="點(diǎn)擊直接修改圖片網(wǎng)址" />
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitCenter"
app:error="@{@color/black}"
app:imageUrl="@{userImg.url}"
app:placeHolder="@{@color/holderColor}" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</layout>
3、自定義事件的觸發(fā)
當(dāng)自定義事件需要的參數(shù) 發(fā)生改變的時(shí)候就會(huì)產(chǎn)生回調(diào)觸發(fā)自定義事件,如圖 則會(huì)發(fā)生加載圖片
@SuppressWarnings("unused")
public class DataBingHolder implements DataBindingFragment.DataBindingFace {
@Override
public View.OnClickListener getImageOnClickListener() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
//獲取視圖綁定的holder ,對(duì)holder 設(shè)置參數(shù)
FragmentDataBindingBinding binding = DataBindingUtil.findBinding(v);
binding.setImageUrl(DataManager.getRandomUrl());
//DataBinding 觸發(fā)自定義事件加載圖片
}
};
}
}
3、事件指定
可以通過(guò)表達(dá)式將事件的處理者進(jìn)行轉(zhuǎn)接,并加入自定義的參數(shù) 如 2、自定義事件中
android:onClick="@{(theview)->event.getDataOnClick(theview,imageUrl)}"
theview 代指當(dāng)前控件 如果指定的方法需要 則必須在當(dāng)前方法中聲明(theview),如果方法參數(shù)不對(duì)則變異失敗
event 需要事先聲明
/**
* 點(diǎn)擊傳參
* url 可以為空
*/
public void getDataOnClick(View view,String url) {
ToastUtil.showToast(view.getContext(), "網(wǎng)址:" + url);
}
4、事件直接修改數(shù)據(jù)源
由于java 傳參時(shí) 傳遞的實(shí)際是地址值,古在 指定的數(shù)據(jù)源不發(fā)生變化時(shí),可以直接修改數(shù)據(jù),會(huì)自發(fā)相應(yīng)的回調(diào)如
android:onClick="@{()->event.setParamsOnClick(userImg)}"
參數(shù)的處理,此處切記不要用new 新增對(duì)象
/**
* 點(diǎn)擊切換顯示的圖片
*/
public void setParamsOnClick(UserImg img) {
String url = DataManager.getRandomUrl();
img.setUrl(url);
String t = url.substring(url.length() - 10, url.length());
LogUtil.d(TAG, "原標(biāo)題:" + img.title + "\n現(xiàn)標(biāo)題:" + t);
img.setTitle(t);
}
附上Demo