Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind layout with several qualifiers

I have 2 layouts: one for v19+ and another for earlier versions. They contain different views with different ids. How can I say Android DataBinding framework that I want to work with both layouts? It generates views only for one layout (selects randomly).

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>

Update:

Finally I've found out the root of the problem. I don't know why but it does not generate *BindingImpl files when use minSdkVersion 21. If specify earlier versions it works as said @yigit

like image 423
LackOfKnowledge Avatar asked Nov 09 '22 17:11

LackOfKnowledge


1 Answers

Data binding will generate a base class that serves as a common interface for both (the variables). Data binding will take care of creating the right instance when you call DataBindingUtil.setContentView(activity, R.layout.tmp_view).

For instance, in your example, it will generate

TempViewBinding, TempViewBindingImpl and TempViewBindingV19Impl. You can check these classes inside <app module>/build/intermediates/classes/<your package>/databinding/ after you compile.

TempViewBinding is the base class and it will have the combination of variables and views for each layout. If you are not seeing them in the IDE, might be an auto-completion bug in AS (please file a bug).

like image 135
yigit Avatar answered Nov 14 '22 22:11

yigit