XML配置
<?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="viewmodel" // 需要和activity中的BP.XXX一致
type="com.test.FeedbackViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/_232638">
<TextView
android:id="@+id/tv_submit"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_44"
android:layout_marginStart="@dimen/dp_38"
android:layout_marginEnd="@dimen/dp_38"
android:layout_marginBottom="@dimen/dp_90"
android:enabled="false"
android:gravity="center"
android:onClick="@{viewmodel.clickSubmit}"
android:text="@string/submit"
android:textSize="@dimen/sp_14"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Activity配置
override fun initVariableId(): Int {
return BR.viewmodel // 这里字段需要跟xml中的配置一致, 否则编译可以成功, 点击事件会无效
}
如果 XML中 variable字段配置了内容, 此两处的变量需要一直, 如果未配置, initVariableId 方法需要返回BP._all