【发布时间】: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>
现在我想将我的数据模型绑定到此布局,但问题是我不知道如何将两个不同的数据模型绑定到 layout1 和 layout2,因为它们都来自一个布局是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