简体   繁体   中英

Android XML Data Binding pass view by id

In order to show a BottomSheetBehavior i need to pass his view like below.

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

termsTitle.setOnClickListener {
    showBottomSheet(tosLayoutBottomSheet)
 }

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 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:

fragment.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. How can I pass an id from a different layout?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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