【问题标题】:How mange DataBinding in layout included and multiple models?如何管理包含布局和多个模型中的数据绑定?
【发布时间】:2018-02-25 12:18:47
【问题描述】:

我有一个布局,其中包含另一个布局,其中有 2 个数据模型,其中一个是共同的。有一个进行绑定和设置模型的活动。但是有些事情让我无法理解,因为我无法让它发挥作用。 我创建了一个示例,我将其发布在下面: 第一个布局是 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 在绑定后没有被 valorized?

编辑:我已根据 Blackbelt 建议修改了布局和活动。

【问题讨论】:

    标签: android layout binding include models


    【解决方案1】:

    我找到了一个我想在这里分享的解决方案,问题出在我怀疑的活动中,这是正确的:

    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);
        }
    }
    

    这是对具有内容布局和多模型的布局进行正确绑定的正确方法。希望这可以帮助其他人

    【讨论】:

    • 关键是使用parentBinding = DataBindingUtil.setContentView(this, R.layout.parent_layout)。 ParentLayoutBinding.inflate(getLayoutInflater()) 不起作用。
    【解决方案2】:

    对于include,您必须使用app:modelName。您必须将xmlns:app="http://schemas.android.com/apk/res-auto" 添加到您的&lt;layout&gt; 标签中

    例如

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

    在哪里

    app:firstModel
    

    firstModel 是要在包含中使用的视图模型的变量的名称,

     @{firstModel}
    

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

    【讨论】:

    • 谢谢@Blackbelt 我做了你说的修改但不幸的是仍然没有工作,我怀疑问题出在活动中
    猜你喜欢
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 2016-03-21
    • 2016-07-24
    相关资源
    最近更新 更多