android - 如何使用多个限定符绑定(bind)布局

标签 android data-binding android-databinding

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

layout/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 所说的那样工作

最佳答案

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

例如,在你的例子中,它会生成

TempViewBinding , TempViewBindingImplTempViewBindingV19Impl . 你可以在<app module>/build/intermediates/classes/<your package>/databinding/里面查看这些类编译后。

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

关于android - 如何使用多个限定符绑定(bind)布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36399697/

相关文章:

android - 带有(架构导航组件)和 BottomNavigationView 的启动画面

java - Gradle 找不到 JAVA_HOME @Linux Mint

java - 如何在应用程序运行时将多个 textView 更改为特定的日期和时间

android - 如何在sqlite数据库中找到列的最大值

c# - 列表框未绑定(bind)到 BindingList<T>

TableLayout 的 Android 数据绑定(bind)

java - Android:检测 ObservableList 中已删除的项目

android - GCM 云连接服务器中上游消息传递的主要用途是什么?

Android 数据绑定(bind)不适用于 View 'android:tag' 属性

wpf - WPF 中的主细节绑定(bind)