我有两种布局:一种用于 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
, TempViewBindingImpl
和 TempViewBindingV19Impl
.
你可以在<app module>/build/intermediates/classes/<your package>/databinding/
里面查看这些类编译后。
TempViewBinding
是基类,它将具有每个布局的变量和 View 的组合。
如果您在 IDE 中没有看到它们,则可能是 AS 中的自动完成错误(请提交错误)。
关于android - 如何使用多个限定符绑定(bind)布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36399697/