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

How to pass OnClickListener to included layout?

我需要将 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 传递给包含的布局?

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

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