简体   繁体   English

Android 数据绑定 - 查看参考

[英]Android Data Binding - Reference to view

I use in my new app the data binding library of android.我在我的新应用程序中使用了 android 的数据绑定库。 Currently I try to pass a reference of another view to a method.目前我尝试将另一个视图的引用传递给一个方法。

I have an ImageButton with an onClickListener .我有一个带有onClickListenerImageButton In this onClick listener I want to pass a reference of the root view to the method.在这个 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>

This source code provided above is only an example and not the complete.上面提供的这个源代码只是一个例子,并不完整。 There are more children and also the image button is not a direct child of the root element.有更多的子元素,而且图像按钮不是根元素的直接子元素。 But I think the meaning is clear.但我认为意思是明确的。

I already tried to pass a reference by specifying the id of the root view (see above).我已经尝试通过指定根视图的 id 来传递引用(见上文)。 But this doesn't work.但这不起作用。 If I try to compile this, I get the error, that the type of root_element is not specified.如果我尝试编译它,我会收到错误,即未指定root_element的类型。

I also tried to import the generated binding class and access the root element by the public field in it.我还尝试导入生成的绑定类并通过其中的公共字段访问根元素。 Also this method doesn't work, since the binding class has to be generated first.此方法也不起作用,因为必须首先生成绑定类。

So is there any way to pass a reference of a view to a method?那么有没有办法将视图的引用传递给方法? I know that I could pass the id of the root view with @id/root_element , but I don't want that, since I have to find a way to get a reference to this view only with the given id.我知道我可以使用@id/root_element传递根视图的@id/root_element ,但我不希望那样,因为我必须找到一种方法来仅使用给定的 id 获取对此视图的引用。

You can use root_element, but Android Data Binding camel-cases the names.您可以使用 root_element,但 Android Data Binding 使用驼峰命名法。 So, root_element becomes rootElement.所以,root_element 变成了 rootElement。 Your handler should be:你的处理程序应该是:

android:onClick="@{() -> Helper.doSth(rootElement)}"

The difference between what you have and what you should do is, don't pass the id root_element .你拥有的和你应该做的之间的区别是,不要传递 id root_element Rather, pass through the view as another variable into the layout file.相反,将视图作为另一个变量传递到布局文件中。

In my case, I had a switch in my layout, that I wanted to pass as a parameter to a method in my lambda.就我而言,我的布局中有一个开关,我想将其作为参数传递给我的 lambda 中的方法。 My code is like this:我的代码是这样的:

MyLayoutBinding binding = DataBindingUtil.inflate(inflater, R.layout.my_layout, parent, true);
binding.setDataUpdater(mDataUpdater);
binding.setTheSwitch(binding.switchFavorite);

Then my layout is like this:然后我的布局是这样的:

<?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"
            ... />
.../>

So as you can see with this, in my code, I get the reference to my switch and pass it in as a variable in the binding.因此,正如您所看到的,在我的代码中,我获取了对我的开关的引用并将其作为绑定中的变量传递。 Then in my layout I then have access to it, to pass it through in my lambda.然后在我的布局中,我可以访问它,在我的 lambda 中传递它。

You should pass the id of the element you want to reference.您应该传递要引用的元素的 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)}" />

See that the switch id is newGamesNotificationSwitch and thats what i am passing into getSubscriptionsValues(..) function.看到 switch id 是 newGamesNotificationSwitch,这就是我传递给 getSubscriptionsValues(..) 函数的内容。

In case your id has some underscores (_) you should pass it using camelcase.如果您的 id 有一些下划线 (_),您应该使用驼峰式大小写传递它。

Eg: my_id_with_underscore should be passed as myIdWithUnderscore.例如:my_id_with_underscore 应该作为 myIdWithUnderscore 传递。

Hope it helps希望它有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM