类似于以下内容:

android:onClick="@{() -> presenter.onClick(rootView)}"

源代码:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>
        <variable
            name="presenter"
            type="Presenter" />
    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{() -> presenter.onClick(rootView)}"
            android:text="abcde" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="tytyt" />
    </LinearLayout>
</layout>

最佳答案

是的,您可以通过其ID获得特定视图

android:onClick="@{() -> presenter.onClick(viewId)}"

在你的情况下你可以做
<LinearLayout android:id="@+id/parentView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="@{() -> presenter.onClick(parentView)}"
        android:text="abcde" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="tytyt" />
</LinearLayout>

然后您的onClick将
public void onClick(View view) or public void onClick(LinearLayout layout)
{

}

关于android - 我们可以在数据绑定(bind)的xml布局中获取根 View 吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45296203/

10-12 00:24
查看更多