[英]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.