Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include tag and dataBinding

I want to use one of my layouts multiple times in the same view using include. Let's say I have a custom.xml including some TextViews.

custom.xml:

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

I have included this layout multiple times in parent.xml:

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/custom"
        android:id="@+id/layout1"/>

    <include layout="@layout/custom"
        android:id="@+id/layout2"/>
</LinearLayout>

Now I want to bind my data models to this layout, but the problem is that I don't know how to bind two different data models to layout1 and layout2 since both of them are refrenced from one layout which is custom.xml. As far as I know I can add this tag in my xml layout:

    <data>
       <variable name="user" type="com.myproject.model.User"/>
   </data>

But I need to bind two different data models to custom.xml.

My question is how to have an included layout multiple times in one view and passing different data to them using Data Binding? something like passing data to the layout but not statically binding a model to an xml.

I also found this question which is exactly had the same problem But since Data Binding is released in newer versions of android I am seeking a way to solve the same issue using Data Binding. Here is the part of that question that I have quoted for clarification:

For instance, I have a carefully crafted layout that I want to display three times in my view. Every of those instances would need different values. Since the include is basically a take that XML and paste it here, I'd need something more powerful.

like image 660
Milad Faridnia Avatar asked Oct 05 '16 06:10

Milad Faridnia


People also ask

Can I use both DataBinding and ViewBinding?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.

How do you use data binding with tag?

Data Binding in <include> Layouts xml layout to enable data binding. The <data> and <variable> tags are used to bind the student object. To pass the user to included content_student_main layout, bind:student=”@{student}” is used. Without this, the user object won't be accessible in content_student_main layout.

When should I use DataBinding?

Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.


2 Answers

You can pass that from parent.xml

<include layout="@layout/custom"
    android:id="@+id/layout1"
    app:user="@{object of user1`}"/>

<include layout="@layout/custom"
    android:id="@+id/layout2"
    app:user="@{object of user2`}"/>

Here you need to pass User object from parent.xml

Make sure you have <data> in custom.xml

<data>
   <variable name="user" type="com.myproject.model.User"/>
</data>

Here is detailed answer related to this, refer it

like image 94
Ravi Avatar answered Oct 09 '22 05:10

Ravi


We know how to use the POJO name and its type on the XML which we are using in setContentView() as a parent view. We should focus on the include tag if we are including any layouts from resource as follows:

<?xml version="1.0" encoding="utf-8"?>

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

    <data>

        <variable
            name="user"
            type="exapmpe.model.User" />
    </data>

    <android.support.design.widget.CoordinatorLayout
        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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <include
            layout="@layout/content_main"
            bind:user="@{user}" />

    </android.support.design.widget.CoordinatorLayout>

</layout>

Here we've use the bind attribute to pass the object to show the detailed info on the content screen. Please make sure the object name should be same in both places like bind:user="@{user}. The content_main.xml should look as follows:

<?xml version="1.0" encoding="utf-8"?>

<layout>

    <data>

        <variable
            name="user"
            type="exapmpe.model.User" />
    </data>

    <RelativeLayout 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"
        android:id="@+id/content_main" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.firstName + user.lastName}" />

    </RelativeLayout>

</layout>
like image 32
Deva Avatar answered Oct 09 '22 04:10

Deva