【问题标题】:Android Data Binding - Reference to viewAndroid 数据绑定 - 参考视图
【发布时间】: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


    【解决方案1】:

    您应该传递要引用的元素的 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 有一些下划线 (_),您应该使用驼峰式来传递它。

    例如: my_id_with_underscore 应作为 myIdWithUnderscore 传递。

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      你所拥有的和你应该做的区别在于,不要传递 id root_element。相反,将视图作为另一个变量传递到布局文件中。

      在我的例子中,我的布局中有一个开关,我想将它作为参数传递给我的 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"
                  ... />
      .../>
      

      因此,正如您所看到的,在我的代码中,我获取了对我的开关的引用并将其作为绑定中的变量传递。然后在我的布局中,我可以访问它,在我的 lambda 中传递它。

      【讨论】:

        【解决方案3】:

        您可以使用 root_element,但 Android Data Binding 会将名称转换为驼峰式。所以,root_element 变成了 rootElement。您的处理程序应该是:

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

        【讨论】:

        • 我认为这应该是答案。简单得多,并且坚持使用 Google 文档。如果人们想了解更多信息,他们可以参考下面的答案和 Google IO 2016。stackoverflow.com/questions/37727600/…youtube.com/…
        • 这个答案是正确的。每当您使用数据绑定来传递视图元素时。默认情况下是驼峰式的。
        猜你喜欢
        • 1970-01-01
        • 2016-01-14
        • 1970-01-01
        • 2020-09-29
        • 2018-06-12
        • 2016-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多