Android - 如何使用数据绑定从 XML 传递视图对象

Android - How to pass object of a view from XML with data binding

我正在构建一个 Android 应用程序,我在其中使用了数据绑定库。我对 Data binding 概念有点陌生,正在寻找以下

的解决方案

有一个名为 otp_validation.xml

的布局文件
<data>
    <variable
        name="otpViewModel"
        type="dial.to.go.otp.OTPViewModel" />
</data>
<ConstraintLayout
 .....................
 .....................
 .....................
<LinearLayout
            android:id="@+id/otp_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_20sdp"
            android:orientation="horizontal"
            android:weightSum="4">

            <androidx.appcompat.widget.AppCompatEditText
                android:layout_width="@dimen/_40sdp"
                android:layout_height="@dimen/_50sdp"
                android:layout_weight="1"
                android:background="@drawable/all_edit_selector"
                android:gravity="center"
                android:inputType="number"
                android:maxLength="1"
                android:textSize="@dimen/_22sdp" />

            <androidx.appcompat.widget.AppCompatEditText
                android:layout_width="@dimen/_40sdp"
                android:layout_height="@dimen/_50sdp"
                android:layout_marginLeft="@dimen/_8sdp"
                android:layout_weight="1"
                android:background="@drawable/all_edit_selector"
                android:gravity="center"
                android:inputType="number"
                android:maxLength="1"
                android:textSize="@dimen/_22sdp" />

</LinearLayout>


        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginTop="@dimen/_30sdp"
            android:background="@color/colorPrimary"
            android:backgroundTint="@color/colorPrimary"
          
          <!-- I want to pass the view object of otp container as a parameter in the method validateOTP()
            android:onClick="@{() -> otpViewModel.validateOTP()}"
           
            android:src="@drawable/drawable_fab_proceed"
            app:borderWidth="0dp"
            app:fabSize="normal"
            app:rippleColor="@color/colorAccent" />
           
</ConstraintLayout>

请注意在 onClick 事件中调用的方法 validateOTP()。我想将线性布局 (otp_container) 的视图对象作为参数发送。请帮我解决这个问题。

将对象绑定到 XML 的逻辑几乎相同,在 activity/fragment class 中创建这些对象,在 [=35= 中初始化相应的变量] 文件并使用绑定来绑定对象和变量。

尝试以下方法

Activity class

val yourLinearLayout = findViewById<View>(R.id.otp_container)
binding.view = yourLinearLayout 

XML 文件

<data>
    <variable
        name="otpViewModel"
        type="dial.to.go.otp.OTPViewModel" />

    <variable
        name = "view"
        type = "android.widget.LinearLayout"
</data>

//rest of the logic

     <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        ..........          
        ..........
        android:onClick="@{() -> otpViewModel.validateOTP(view)}"
        ..........
        ........./>

虽然有效,但确实是一种不好的做法。 Viewgroup 必须对视图 一无所知 因为如果 activity/fragment 得到 destroyed/recreated,视图组中的视图引用将导致内存泄漏

要通过点击的视图就可以了

android:onClick="@{(view)->viewModel.onOthers(view)}"

要传递点击视图以外的视图,传递id,

android:onClick="@{(view)->viewModel.onOthers(otpContainer)}"

注意方法的签名应该是这样的

fun onOthers(view:View){
  //your code goes here
}

以便它可以允许任何类型的视图。

更新 对于 Binding

otp_container 将是 otpContainer