How to associate a layout with multiple qualifiers

I have 2 layouts: one for v19 + and the other for earlier versions. They contain different types with different identifiers. How can I tell the Android DataBinding framework that I want to work with both layouts? It generates views for only 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 found out the root of the problem. I don’t know why, but it does not generate * BindingImpl files when using minSdkVersion 21. If you specify earlier versions, it works as @yigit said

+5
source share
1 answer

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

For example, 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 compilation.

TempViewBinding is a base class that will have a combination of variables and views for each layout. If you do not see them in the IDE, it may be an autocompletion error in AS (please indicate an error).

+1
source

Source: https://habr.com/ru/post/1246397/


All Articles