包括标签和数据绑定

Include tag and dataBinding

我想使用 include 在同一视图中多次使用我的布局之一。假设我有一个 custom.xml 包括一些 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>

我在 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>

现在我想将我的数据模型绑定到这个布局,但问题是我不知道如何将两个不同的数据模型绑定到 layout1layout2,因为它们都是从 custom.xml 的一种布局中引用。据我所知,我可以在我的 xml 布局中添加这个标签:

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

但我需要将两个不同的数据模型绑定到 custom.xml

我的问题是如何在一个视图中多次包含一个布局并使用数据绑定将不同的数据传递给它们?类似于将数据传递给布局但不将模型静态绑定到 xml.

我还发现 this 问题完全相同但是由于数据绑定在 android 的较新版本中发布,我正在寻找一种使用数据绑定解决相同问题的方法。这是我为了澄清而引用的那个问题的一部分:

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.

您可以通过 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`}"/>

这里需要从parent.xml

传递User对象

确保 custom.xml

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

是这方面的详细解答,参考一下

问题是包含的布局未被视为数据绑定布局,了解如何解决它

我们知道如何在我们在 setContentView() 中用作父视图的 XML 上使用 POJO 名称及其类型。如果我们包含资源中的任何布局,我们应该关注 include 标签,如下所示:

<?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>

这里我们使用 bind 属性来传递对象以在内容屏幕上显示详细信息。请确保对象名称在两个地方都应该相同,例如bind:user="@{user}content_main.xml 应如下所示:

<?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>