包括标签和数据绑定

问题描述 投票:0回答:4
android xml android-layout android-databinding
4个回答
63
投票

您可以从

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`}"/>

这里需要从

User
 传递 
parent.xml

对象

确保 custom.xml 中有

<data>

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

这里有相关详细解答,参考一下


18
投票

我们知道如何在

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
投票

问题是包含的布局不被认为是数据绑定布局,看看这里了解如何解决它


0
投票

1> 在使用 include 标签的主布局屏幕中添加 xmlns:bind="http://schemas.android.com/apk/res-auto" 此代码位于布局标签下。请查看屏幕截图供您参考。

point 1 reference screenshort

2> 现在在您的 include 标签下添加 bind:includeLayoutViewModel="@{viewModel}" (在本例中添加您声明的 ViewModel 名称)有关更多信息,请查看屏幕截图

For point 2 reference screenshort

3> 现在,在您包含的布局屏幕中,在此处调用 ViewModel 以供参考,检查下面的屏幕截图(在屏幕截图中,有 TestViewModel 代表您提到您在主布局屏幕中声明的视图模型)

For point 3 screenshort

© www.soinside.com 2019 - 2024. All rights reserved.