【发布时间】:2016-10-30 20:54:21
【问题描述】:
我在我的新应用中使用了 android 的数据绑定库。 目前我尝试将另一个视图的引用传递给方法。
我有一个ImageButton 和一个onClickListener。在这个 onClick 监听器中,我想将根视图的引用传递给方法。
<RelativLayout
android:id="@+id/root_element"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:contentDescription="@string/close_dialog"
android:src="@drawable/ic_close_212121_24dp"
android:background="@android:color/transparent"
android:onClick="@{() -> Helper.doSth(root_element)}"/>
</RelativLayout>
上面提供的这个源代码只是一个例子而不是完整的。 有更多的子元素,并且图像按钮不是根元素的直接子元素。不过我觉得意思很清楚。
我已经尝试通过指定根视图的 id 来传递引用(见上文)。但这不起作用。如果我尝试编译它,我会收到错误,即未指定 root_element 的类型。
我还尝试导入生成的绑定类并通过其中的公共字段访问根元素。这个方法也不起作用,因为必须先生成绑定类。
那么有没有办法将视图的引用传递给方法?
我知道我可以使用 @id/root_element 传递根视图的 id,但我不希望这样,因为我必须找到一种方法来仅使用给定的 id 获取对该视图的引用。
【问题讨论】:
标签: android data-binding android-view