如何在布局中包含mange DataBinding和多个模型?

时间:2018-02-25 12:18:47

标签: android layout binding include models

我的布局包含另一个布局,其中包含两个数据模型,其中一个是共同的。有一个Activity来进行绑定并设置模型。但有些事情让我感到懊恼,因为我无法让它发挥作用。 我创建了一个示例,我在下面发布它: 第一个布局是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>

第一个布局中包含的第二个布局是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>

使用DataBinding的Activity是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);
    }
}

最后模型是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;
    }

}

和SecondDataModel.java是:

package com.example.databinding.model;

public class SecondDataModel {
    private String thirdMsg;

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

    public String getThirdMsg() {
        return thirdMsg;
    }
}

为什么布局中的TextView在绑定后没有增值?

编辑:我已经使用Blackbelt建议修改了布局和活动。

1 个答案:

答案 0 :(得分:0)

对于include您必须使用app:modelName。您必须将xmlns:app="http://schemas.android.com/apk/res-auto"添加到<layout>代码

E.g。

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

中的位置
app:firstModel

firstModel是要在include和

中使用的视图模型的变量的名称
 @{firstModel}

当然是您要传递的ViewModel。您可以以相同的方式轻松传递多个视图模型。