将通用列表传递到<include>

时间:2016-09-02 13:29:14

标签: android android-databinding

有两个复杂的布局,一个包含在另一个中。 尝试将一些自定义对象的列表传递到包含的布局中,但构建失败并出现以下错误:

Error:Execution failed for task ':core:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:cannot find type element for List
file:D:\myproject-android\core\src\main\res\layout\view_header.xml
loc:101:27 - 101:42
****\ data binding error ****

view_header.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="vm"
            type="com.catalogz.app.ui.categories.ItemViewModel" />
    </data>
    <FrameLayout>
       <include layout="@layout/include_title_view" bind:items="@{vm.items}/>
    </FrameLayout>
   ...
</layout>

include_title_view.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="com.catalogz.app.utils.Strings" />
        <import type="com.catalogz.app.app.entities.Item" />
        <import type="java.util.List"/>
        <variable
            name="items"
            type="List&lt;Item&gt;" /> <!-- generic List<Item> -->
    </data>
    <TextView android:text="@{Strings.convert(items)}"/>
</layout>

可以通过删除通用定义&lt;Item&gt;将此问题从 view_header.xml 移至 include_title_view.xml ,但它会显示在Strings.convert(items)中如我所知,调用as方法需要Item列表和布局传递List<Object>

Error:Execution failed for task ':core:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:cannot find method convert(java.util.List) in class com.catalogz.app.utils.Strings
file:D:\myproject-android\core\src\main\res\layout\include_title_view.xml
loc:51:32 - 51:61
****\ data binding error ****

1 个答案:

答案 0 :(得分:0)

我也有同样的问题,但我找到了一个替代方法,用于在包含的布局中设置值。我知道这不是一个好习惯,至少它是为包含布局和数据绑定的设置变量的工作。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="vm"
            type="com.catalogz.app.ui.categories.ItemViewModel" />
    </data>
    <FrameLayout>
       <include 
    android:id="@+id/part1"
    layout="@layout/include_title_view" />
    </FrameLayout>
   ...
</layout>

在Activity类

ActivityMainBinding.part1.setItems(myItems);

希望这会有所帮助。