【问题标题】:Include tag and dataBinding包括标签和数据绑定
【发布时间】:2017-02-13 11:55:49
【问题描述】:

我想使用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 中发布的,因此我正在寻找一种使用数据绑定解决相同问题的方法。这是我为澄清而引用的那个问题的一部分:

例如,我想展示一个精心设计的布局 在我看来是三倍。这些实例中的每一个都需要不同的 价值观。由于include 基本上是获取 XML 并粘贴它 在这里,我需要更强大的东西。

【问题讨论】:

  • 如果布局相同,只有数据在变化,为什么不考虑使用自定义recycleView?
  • 因为我想传递两个不同的数据模型,例如一个是用户,另一个是地址,但两个模型都使用相同的布局来显示它们的数据
  • @MiladFaridnia 嘿,先生,你能帮我解决给出的链接问题评论吗? stackoverflow.com/questions/35554796/…

标签: android xml android-layout android-databinding


【解决方案1】:

我们知道如何在 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>

【讨论】:

    【解决方案2】:

    问题是包含的布局没有被认为是数据绑定布局,take a look here 了解如何解决它

    【讨论】:

    • 感谢您提供有用的链接,但我认为您必须在回答中包含更多描述
    【解决方案3】:

    你可以通过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 中有 &lt;data&gt;

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

    Here是与此相关的详细答案,参考一下

    【讨论】:

    • 感谢您的回答它与最终答案非常接近,但是是否可以将两种模型(例如一种是用户,另一种是地址)传递给 custom.xml ?
    • 是的,在这种情况下你需要在custom.xml中取两个变量
    • 这样的?
    • 首先我想知道为什么这会起作用,因为我还没有为app:user 定义绑定。但它就像一个魅力!谢谢
    • 如果我想传入两个参数怎么办,它不起作用
    猜你喜欢
    • 2016-01-02
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多