Android 使用 MVVMHabit XML中配置OnClick失效问题

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值