簡體   English   中英

Android XML Data Binding pass view by id

[英]Android XML Data Binding pass view by id

為了顯示 BottomSheetBehavior,我需要像下面這樣傳遞他的視圖。

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

片段.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>

片段.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>

因此,改為使用片段中的 onclicklistener 我想從我的 xml 和 viewmodel 中做到這一點:

片段.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" />

我知道上述方法不起作用,因為 tosLayoutBottomSheet id 在另一個布局中。 如何從不同的布局傳遞 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" />

然后在你 onclick

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM