Search code examples
androidlayoutbindingincludemodels

How mange DataBinding in layout included and multiple models?


I have a layout that include another layout with 2 data model of which one in common. Have an Activity that make binding and sets the models. But there is something that escapes me because I just can't make it work. I created an example, I publish it below: The first layout is parent_layout.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="firstModel"
            type="com.example.databinding.model.FirstDataModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/first_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{firstModel.firstMsg}"
            tools:text="firstMsg"/>

        <include
            app:firstModel="@{firstModel}"
            android:id="@+id/container"
            layout="@layout/child_layout"/>

    </LinearLayout>
</layout>

The second layout included in first layout is child_layout.xml :

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="firstModel"
            type="com.example.databinding.model.FirstDataModel" />

        <variable
            name="secondModel"
            type="com.example.databinding.model.SecondDataModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/second_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{firstModel.secondMsg}"
            tools:text="secondMsg"/>

        <TextView
            android:id="@+id/third_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{secondModel.thirdMsg}"
            tools:text="thirdMsg"/>

    </LinearLayout>
</layout>

The Activity that use DataBinding is IncludeLayoutActivity.java:

package com.example.databinding;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.binding.R;
import com.example.binding.databinding.ChildLayoutBinding;
import com.example.binding.databinding.ParentLayoutBinding;
import com.example.databinding.model.FirstDataModel;
import com.example.databinding.model.SecondDataModel;

public class IncludeLayoutActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent_layout);

        ParentLayoutBinding parentBinding = ParentLayoutBinding.inflate(getLayoutInflater());
        ChildLayoutBinding childBinding = ChildLayoutBinding.inflate(getLayoutInflater());

        FirstDataModel firstDataModel = new FirstDataModel("Hello", "Android");
        SecondDataModel secondDataModel = new SecondDataModel("World");

        parentBinding.setFirstModel(firstDataModel);
        childBinding.setSecondModel(secondDataModel);
    }
}

And finally the models are FirstDataModel.java:

package com.example.databinding.model;

public class FirstDataModel {
    private String firstMsg;
    private String secondMsg;

    public FirstDataModel(String hello, String android) {
        firstMsg = hello;
        secondMsg = android;
    }

    public String getFirstMsg() {
        return firstMsg;
    }

    public String getSecondMsg() {
        return secondMsg;
    }

}

and SecondDataModel.java is:

package com.example.databinding.model;

public class SecondDataModel {
    private String thirdMsg;

    public SecondDataModel(String world) {
        thirdMsg = world;
    }

    public String getThirdMsg() {
        return thirdMsg;
    }
}

Why the TextView in layouts are not valorized after binding?

EDIT: I have modified layout and activity with Blackbelt suggestion.


Solution

  • I found a solution I want to share here, the problem was in the Activity as I suspected, this is the correct one:

    package com.example.databinding;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    
    import com.example.binding.R;
    import com.example.binding.databinding.ChildLayoutBinding;
    import com.example.binding.databinding.ParentLayoutBinding;
    import com.example.databinding.model.FirstDataModel;
    import com.example.databinding.model.SecondDataModel;
    
    public class IncludeLayoutActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            ParentLayoutBinding parentBinding = DataBindingUtil.setContentView(this, R.layout.parent_layout);
    
            FirstDataModel firstDataModel = new FirstDataModel("Hello", "Android");
            SecondDataModel secondDataModel = new SecondDataModel("World");
    
            parentBinding.setFirstModel(firstDataModel);
            childBinding.setSecondModel(secondDataModel);
        }
    }
    

    This is the right way to have the correct binding for a layout with a content layout and multi model. Hope this can help someone other