Android数据绑定-查看参考
我在我的新应用程序中使用了android的数据绑定库。 目前,我尝试将另一个视图的引用传递给一个方法 我有一个Android数据绑定-查看参考,android,data-binding,android-view,Android,Data Binding,Android View,我在我的新应用程序中使用了android的数据绑定库。 目前,我尝试将另一个视图的引用传递给一个方法 我有一个ImageButton和一个onClickListener。在这个onClick侦听器中,我想将根视图的引用传递给该方法 <RelativLayout android:id="@+id/root_element" android:layout_width="match_parent" android:layout_height="match_parent"&
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\u元素
的类型没有指定
我还尝试导入生成的绑定类,并通过其中的公共字段访问根元素。此外,此方法也不起作用,因为必须首先生成绑定类
那么有没有办法将视图的引用传递给方法呢?
我知道我可以通过
@id/root\u元素
传递根视图的id,但我不希望这样,因为我必须找到一种方法,只通过给定的id获取对该视图的引用。您拥有的和您应该做的之间的区别是,不要传递id根元素
。而是将视图作为另一个变量传递到布局文件中
在我的例子中,我的布局中有一个开关,我想把它作为参数传递给lambda中的一个方法。我的代码如下:
MyLayoutBinding binding = DataBindingUtil.inflate(inflater, R.layout.my_layout, parent, true);
binding.setDataUpdater(mDataUpdater);
binding.setTheSwitch(binding.switchFavorite);
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="dataUpdater" type="..."/>
<variable name="theSwitch" type="android.widget.Switch"/>
<import type="android.view.View"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{()->dataUpdater.doSomething(theSwitch)}">
<Switch
style="@style/Switch"
android:id="@+id/switch_favorite"
... />
.../>
那么我的布局是这样的:
MyLayoutBinding binding = DataBindingUtil.inflate(inflater, R.layout.my_layout, parent, true);
binding.setDataUpdater(mDataUpdater);
binding.setTheSwitch(binding.switchFavorite);
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="dataUpdater" type="..."/>
<variable name="theSwitch" type="android.widget.Switch"/>
<import type="android.view.View"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{()->dataUpdater.doSomething(theSwitch)}">
<Switch
style="@style/Switch"
android:id="@+id/switch_favorite"
... />
.../>
.../>
如您所见,在我的代码中,我获取了对我的开关的引用,并将其作为绑定中的变量传入。然后在我的布局中,我可以访问它,在我的lambda中传递它。您可以使用root\u元素,但是Android数据绑定会将名称隐藏起来。所以,根元素成为根元素。您的处理程序应该是:
android:onClick="@{() -> Helper.doSth(rootElement)}"
您应该传递要引用的元素的id
<data>
<variable
name="viewModel"
type=".....settings.SettingsViewModel" />
</data>
.
.
.
<Switch
android:id="@+id/newGamesNotificationSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{viewModel.getSubscriptionsValues(newGamesNotificationSwitch)}" />
.
.
.
请注意开关id是newGamesNotificationSwitch,这就是我传递给getSubscriptionsValues(..)函数的内容
如果您的id有一些下划线(u),您应该使用camelcase传递它
例如:
带有下划线的my\u id\u应作为MyID with下划线传递
希望有帮助我认为这应该是答案。更简单,与谷歌文档保持一致。想要了解更多信息,请参考以下答案和谷歌IO 2016。这个答案是正确的。无论何时使用数据绑定传递视图元素。默认情况下,它是驼峰式的。