繁体   English   中英

Android - 如何通过数据绑定从 XML 传递视图的 object

[英]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)的视图 object 作为参数发送。 请帮助我为我提供解决方案。

将对象绑定到 XML 的逻辑几乎相同,在活动/片段 class 中创建这些对象,初始化 XML 文件中的相应变量并使用绑定变量。

尝试以下

活动 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必须对视图一无所知,因为如果活动/片段被破坏/重新创建,视图组中的视图引用将导致 memory 泄漏

要传递单击的视图,您可以这样做

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

要传递单击视图以外的视图,请传递 id,

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

请注意,方法的签名应该像

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

这样它就可以允许任何类型的视图。

更新otp_container将是otpContainer用于Binding

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM