<data> <import type="android.view.View"/> <variable name="data" type="com.x.x.x.Bean" /> <variable name="listener" type="android.view.View.OnClickListener" /> </data> <!--要在data節點下引入view--> android:visibility="@{data.isCharge ? View.VISIBLE: View.GONE}">
binding中的數據data類對應的JavaBean類bean,bean的屬性修改後從新賦值給binding,就能夠通知UI更改綁定的視圖,實現數據和視圖的動態綁定java
bean = xxBean("數據綁定",false) mBinding.data = bean //javabean的值更改,視圖層UI也就是xml文件中引用data的地方也隨之修改
open class BaseDto : Serializable { } data class SupportCodeDes( var codeList:List<String>? = arrayListOf() ):BaseDto()
var codeList:MutableLiveData<SupportCodeDes> = MutableLiveData() fun getSupportCode(){ var list = VehicleConfig.getAllFunction() var supportCodeDes = SupportCodeDes(list) codeList.postValue(supportCodeDes) }
override fun initData() { mModel.getSupportCode() mModel.codeList.observe(this, Observer { logE("更新UI ${it.codeList}") mBinding.code = it }) }
<data> <import type="android.view.View" /> <variable name="listener" type="android.view.View.OnClickListener" /> <variable name="code" type="com.beans.vehicle.dto.SupportCodeDes" /> </data> <LinearLayout android:id="@+id/ll_find_car" android:onClick="@{listener}" android:visibility="@{code.codeList.contains(`1-1-3`)==true?View.VISIBLE:View.GONE}" style="@style/RemoteShowLineCenter">
mBinding.listener = View.OnClickListener { when(it.id){ R.id.tv_temperature -> showAirControlDialog() } }
數據改變時同時使視圖刷新,例如EditText的綁定
綁定變量的方式比單向綁定多了一個等號:android:text="@={user.name}"android
<data> <variable name="userInfo" type="com.github.ixiaow.sample.model.ObservableUser"/> </data> <EditText app:layout_constraintVertical_chainStyle="spread" android:id="@+id/mUserName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@={userInfo.name, default=`name`}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/>
當 EditText的輸入內容改變時,會同時同步到變量 usergit
android:onClick="@{onClickUtil::onClickWithMe}"
手動導入BRgithub
android:text="@{`您的愛車`+myData.licenseNumber+`很是健康,繼續保持哦`}"
'&' --> '&' '<' --> '<' '>' --> '>'
android:visibility="@{data.isOwner()==true && data.isDefault()==false ? View.VISIBLE:View.GONE}"