使用DataBinding将ObservableArrayList <modelclass>设置为RecyclerView

时间:2018-04-06 19:53:19

标签: android android-databinding

我想在API调用后更新RecyclerView中的数据项。我使用过ObservableArrayList。在api调用之后,我在setter方法中使用了notifyChange()。当我将ObservableArrayList传递给XML时,我收到的错误是这样的:

msg:Cannot find the setter for attribute 'bind:categories_list' with parameter type android.databinding.ObservableArrayList<T> on android.support.v7.widget.RecyclerView

XML是:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="list"
            type="ObservableArrayList" />

        <import type="android.databinding.ObservableArrayList" />
    </data>

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:bind="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/white">

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                bind:categories_list="@{list}" />
        </android.support.v7.widget.CardView>
    </android.support.constraint.ConstraintLayout>
</layout>

1 个答案:

答案 0 :(得分:0)

您需要使用处理列表的@BindingAdapter("categories_list")注释。查看this answer了解详情

修改

不确定您是否采用了正确的方法。语法看起来很怪异。通常在这里:

<variable
    name="list"
    type="ObservableArrayList" />

是你自己的模型类,导入它并将它与两个声明一起使用对我来说也很奇怪,你试过了吗?

<variable
    name="list"
    type="android.databinding.ObservableArrayList" />