【问题标题】:How to bind layout with several qualifiers如何将布局与多个限定符绑定
【发布时间】:2016-07-23 19:41:03
【问题描述】:

我有两种布局:一种用于 v19+,另一种用于早期版本。它们包含具有不同 ID 的不同视图。我怎么能说我想同时使用这两种布局的 Android DataBinding 框架?它只为一种布局生成视图(随机选择)。

布局/temp_view.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="block"
            type="ru.temp.model.content.blocks.WebMediaBlock" />

        <import type="ru.temp.model.Types.ProviderTypes" />

    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:background="@android:color/white">

        <ImageView
            android:id="@+id/provider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="@dimen/size5"
            android:src="@{ProviderTypes.fromString(block.provider).getResId()}" />
    </FrameLayout>
</layout>

layout-v19/temp_view.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="block"
            type="ru.temp.model.content.blocks.WebMediaBlock" />

        <import type="ru.temp.model.Types.ProviderTypes" />

    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:background="@android:color/white">
        <ru.temp.utils.EmbedView
            android:id="@+id/media_embed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ru.temp.structure.static_material.CreditsView
            android:id="@+id/credits_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>
</layout>

更新:

我终于找到了问题的根源。我不知道为什么,但是在使用 minSdkVersion 21 时它不会生成 *BindingImpl 文件。如果指定早期版本,它就像@yigit 所说的那样工作

【问题讨论】:

  • 您也可以查看this教程供您参考。
  • 不涵盖问题详情

标签: android data-binding android-databinding


【解决方案1】:

数据绑定将生成一个基类,作为两者(变量)的公共接口。当您调用 DataBindingUtil.setContentView(activity, R.layout.tmp_view 时,数据绑定将负责创建正确的实例。

例如,在您的示例中,它将生成

TempViewBindingTempViewBindingImplTempViewBindingV19Impl。 编译后可以在&lt;app module&gt;/build/intermediates/classes/&lt;your package&gt;/databinding/里面查看这些类。

TempViewBinding 是基类,它将具有每个布局的变量和视图的组合。 如果您在 IDE 中没有看到它们,则可能是 AS 中的自动完成错误(请提交错误)。

【讨论】:

  • 不幸的是它不会生成这样的 Impl 类。而且我在官方文档中找不到您提供的信息。无法确定我做错了什么
  • 如果你可以上传一个示例项目,我很高兴看到它
  • 很抱歉回复晚了,但它有很多时间来检测问题。请检查头部的更新。并查看测试项目(master分支)github.com/rchugunov/buzzer在当前版本数据绑定不生成Impl文件。但是如果你在 build.gradle 中设置 minSdkVersion 15 就可以了!
  • 您正在使用 android-apt。尝试在那里报告。
猜你喜欢
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 2020-02-29
  • 2012-09-14
相关资源
最近更新 更多