我们可以在 Databinding 的 xml 布局中获得 Root View 吗?
Can we get Root View in an xml layout in Databinding?
类似于以下内容:
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: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)
{
}