包含标签和数据绑定 [英] Include tag and dataBinding

查看:65
本文介绍了包含标签和数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用include在同一视图中多次使用我的布局之一.假设我有一个custom.xml,其中包括一些TextView.

I want to use one of my layouts multiple times in the same view using include. Let's say I have a custom.xml including some 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中多次包含了此布局:

I have included this layout multiple times in 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布局中添加此标签:

Now I want to bind my data models to this layout, but the problem is that I don't know how to bind two different data models to layout1 and layout2 since both of them are refrenced from one layout which is custom.xml. As far as I know I can add this tag in my xml layout:

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

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

But I need to bind two different data models to custom.xml.

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

My question is how to have an included layout multiple times in one view and passing different data to them using Data Binding? something like passing data to the layout but not statically binding a model to an xml.

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

I also found this question which is exactly had the same problem But since Data Binding is released in newer versions of android I am seeking a way to solve the same issue using Data Binding. Here is the part of that question that I have quoted for clarification:

例如,我有一个精心制作的布局要显示 我认为三遍.这些实例中的每一个都需要不同的 价值观.由于include基本上是将XML粘贴到 在这里,我需要更强大的功能.

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

确保custom.xml中有<data>

Make sure you have <data> in custom.xml

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

此处是与此相关的详细答案,请参考

Here is detailed answer related to this, refer it

这篇关于包含标签和数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆