如何将OnClickListener传递给包含的布局?

时间:2019-07-14 12:44:07

标签: android android-databinding

我需要将OnClickListener从父级传递到包含的布局。

我有一个包含包含接受OnClickListener的视图的布局:

<?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">

 <data>

    <variable
        name="clickListener"
        type="android.view.View.OnClickListener" />

    </data>

         <include // I need to pass here
            android:id="@+id/button_more"
            layout="@layout/button_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

以及随附的布局(button_more.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">
<data>

    <variable
        name="listener"
        type="android.view.View.OnClickListener" />
</data>

<ImageButton
    android:id="@+id/button_more"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?selectableItemBackgroundBorderless"
    android:onClick="@{listener}"
    android:padding="12dp"
    android:src="@drawable/ic_more_vert_black_24dp"
    app:tintColor="@{android.R.attr.textColorPrimary}" />

如何将OnClickListener传递到包含的布局?

1 个答案:

答案 0 :(得分:1)

U可以通过以下方式将clickListener从父级传递到包含的布局:

<include layout="@layout/button_more"
         app:listener="@{clickListener}"/>