android - 将通用列表传递到 <include>

标签 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>

可以通过删除通用定义将此问题从 view_header.xml 移至 include_title_view.xml &lt;Item&gt;但随后它出现在 Strings.convert(items) 中作为方法调用需要列表 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 ****

最佳答案

我也有同样的问题,但我找到了在包含的布局中设置值的替代方法。我知道这不是一个好的做法,至少它是解决带有数据绑定(bind)的包含布局的设置变量的方法。

<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);

希望这对您有所帮助。

关于android - 将通用列表传递到 <include>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39293644/

相关文章:

Android:用图像替换相机预览

javascript - 如何减少 phonegap 应用程序加载外部页面的响应时间?

android - 使用 MVVM 架构的 ViewModel 注入(inject)(在 View 内部)

validation - Android Databinding TextEdit Validation of two or more fields enable button

java - Android 数据绑定(bind)中何时计算表达式?

android - 我可以通过 android 中的 findViewById() 方法直接访问包含布局的 child 吗?

android - 应用程序小部件安卓

android - Youtube Android API 未初始化

android - gradle 3.2.1 的数据绑定(bind)问题

java - 数据绑定(bind): Missing required view with ID