自定义视图的DatabBinding

class CouponsView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
    //名称与UI相同 ,当XML里面用layout标签之后,就会生成这个绑定类
    var mDataBinding : ViewCouponsBinding?=null

    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0) {
        val inflater =
            getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            //绑定布局并显示
        mDataBinding = ViewCouponsBinding.inflate(inflater, this, true);
        //设定布局中的变量
        mDataBinding!!.couponsViewModel = CouponsViewModel()
    }

    constructor(context: Context?) : this(context, null) {}
}
<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
     <data>
        <variable
        <!-- 这里设定什么name,就可以在上面的mDataBinding里,set相应的变量值 -->
            name="couponsViewModel"
            type="com.hxpl.base.weight.CouponsView.CouponsViewModel" />
    </data>
    <LinearLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="68dp"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    </LinearLayout>
</layout>