I have a layout that includes another layout with 2 data models, of which one is common. Have an operation that does the binding and sets the models. But there is something that eludes me, because I just can't get it to work. I created an example, I set it out 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 the 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>
Activity using DataBinding - 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 FirstDataModel.java models:
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:
package com.example.databinding.model;
public class SecondDataModel {
private String thirdMsg;
public SecondDataModel(String world) {
thirdMsg = world;
}
public String getThirdMsg() {
return thirdMsg;
}
}
Why is TextView in layouts not evaluated after binding?
EDIT: I changed the layout and activity with the Blackbelt proposal.