包含tag和dataBinding

Mil*_*nia 28 xml android android-layout android-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>
Run Code Online (Sandbox Code Playgroud)

我已将此布局多次包含在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>
Run Code Online (Sandbox Code Playgroud)

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

    <data>
       <variable name="user" type="com.myproject.model.User"/>
   </data>
Run Code Online (Sandbox Code Playgroud)

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

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

我也发现这个问题确实存在同样的问题但是由于数据绑定是在较新版本的android中发布的,我正在寻找一种方法来解决使用数据绑定的相同问题.以下是我引用澄清的问题的一部分:

例如,我有一个精心设计的布局,我希望在我的视图中显示三次.每个实例都需要不同的值.由于include基本上是XML并将其粘贴到此处,因此我需要更强大的功能.

Rav*_*avi 45

你可以传递它 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`}"/>
Run Code Online (Sandbox Code Playgroud)

在这里你需要传递User对象parent.xml

确保你有<data>custom.xml

<data>
   <variable name="user" type="com.myproject.model.User"/>
</data>
Run Code Online (Sandbox Code Playgroud)

以下是与此相关的详细答案,请参阅


Dev*_*eva 13

我们知道如何在我们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>
Run Code Online (Sandbox Code Playgroud)

在这里,我们使用该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>
Run Code Online (Sandbox Code Playgroud)