简体   繁体   English

Android XML Data Binding pass view by id

[英]Android XML Data Binding pass view by id

In order to show a BottomSheetBehavior i need to pass his view like below.为了显示 BottomSheetBehavior,我需要像下面这样传递他的视图。

ViewUtil.kt class ViewUtil.kt class

 fun showBottomSheet(view: View) {
        val bottomSheetBehavior = BottomSheetBehavior.from(view)

        if (bottomSheetBehavior.state == BottomSheetBehavior.STATE_COLLAPSED) {
            bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
        } else {
            bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
        }
    }

Fragment.kt片段.kt

termsTitle.setOnClickListener {
    showBottomSheet(tosLayoutBottomSheet)
 }

bottom_sheet_tos.xml bottom_sheet_tos.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
    android:id="@+id/tosLayoutBottomSheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:behavior_hideable="true"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

     <!-- some stuff about terms of service -->
</androidx.constraintlayout.widget.ConstraintLayout>

fragment.xml片段.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"
tools:ignore="MissingDefaultResource">

<data>

    <import
        alias="v"
        type="android.view.View" />

    <variable
        name="fragmentViewModel"
        type=".FragmentViewModel"/>
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_main_page">

        <TextView
        android:id="@+id/terms_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="24dp"
        android:layout_marginBottom="8dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:text="@string/terms_of_service"
        android:textColor="@color/colorGrey"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

So, instead use onclicklistener from fragment I would like to do it from my xml and viewmodel like:因此,改为使用片段中的 onclicklistener 我想从我的 xml 和 viewmodel 中做到这一点:

fragment.xml片段.xml

<TextView
        android:id="@+id/terms_title"

        android:onClick="@{(v)-> fragmentViewModel.showBottomSheetFrag(tosLayoutBottomSheet)}"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="24dp"
        android:layout_marginBottom="8dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:text="@string/terms_of_service"
        android:textColor="@color/colorGrey"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

I know the above approach not work because tosLayoutBottomSheet id is in another layout.我知道上述方法不起作用,因为 tosLayoutBottomSheet id 在另一个布局中。 How can I pass an id from a different layout?如何从不同的布局传递 id?

A bit late but you can add an import of R class in your xml file and then pass the view id with R.id.view_id A bit late but you can add an import of R class in your xml file and then pass the view id with R.id.view_id

<import type="com.xxx.xxxx.R" />

then in you onclick然后在你 onclick

android:onClick="@{(v)->view.yourMethod(R.id.view_id)}"

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

相关问题 Android - 如何通过数据绑定从 XML 传递视图的 object - Android - How to pass object of a view from XML with data binding Android - 在 XML 复合视图组件中创建新的视图属性并传递给其子级 - 布局中的数据绑定 - Android - Make new view attribute and pass to its children in XML compound view component - data binding in layout Android - 在数据绑定适配器中传递视图 - Android - Pass view in Data Binding Adapter 如何将数据绑定变量作为 xml 属性传递给自定义视图 - How to pass data binding variable as xml attribute to custom view Android 数据绑定从 onClick 传递不同的视图 - Android Data Binding pass different view from onClick 无法在Android数据绑定中引用其他View ID - Cannot refer to other View ID in Android data binding 如何在Android Studio的数据绑定中将值传递给android.view.View.OnClickListener的变量? - How to pass a value to the variable of android.view.View.OnClickListener in data binding in Android Studio? Android数据绑定XML错误 - Android Data Binding XML Error Android xml中数据绑定比较 - Android Data Binding comparison in xml 如何使用 xml 中的视图引用将参数传递给使用数据绑定的函数? - How to use a view's reference in xml to pass arguments to a function using data binding?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM