android - 数据绑定(bind) RecyclerView : Cannot find the setter for attribute 'app:items'

标签 android data-binding android-databinding

当我尝试竞标 RecyclerView 时出现此错误

Error:(15, 22) Cannot find the setter for attribute 'app:items' with parameter type android.databinding.ObservableArrayList<com.toong.databindingdemo.recycler.UserViewModel on android.support.v7.widget.RecyclerView.

这是我的代码:

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

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="usersViewModel"
            type="com.toong.databindingdemo.recycler.UsersViewModel" />
    </data>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/activity_users_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:items="@{usersViewModel.users}"
        />
</layout>

但是在UsersViewModel我已经有一个公共(public)用户数组

package com.toong.databindingdemo.recycler;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.ObservableArrayList;

public class UsersViewModel extends BaseObservable{
    @Bindable
    public ObservableArrayList<UserViewModel> users;

    public UsersViewModel()
    {
        this.users = new ObservableArrayList<>();
    }
}

我已经清理并重建了项目,但它仍然无法正常工作。我该如何解决这个错误?

最佳答案

RecyclerView 中没有类似app:items 的属性。您需要为此创建自定义绑定(bind)方法

@BindingAdapter("items")
public static void entries(RecyclerView recyclerView, String[] array) {
    //write your code to set RecyclerView adapter.
}

array 的类型更改为所需的数据类型。

关于android - 数据绑定(bind) RecyclerView : Cannot find the setter for attribute 'app:items' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40396040/

相关文章:

android - 为什么右键不能像左键一样在父级的右边

c# - 使用基于 ReactiveUI C# 的绑定(bind)绑定(bind)到 DataGridColumn

c# - WinForms:DataGridView - 编程排序

android - DataBinding 导致 Kotlin 编译器错误

android - 找不到带有参数的属性的 setter

android - 如何根据 sdk 版本在 Android 中启用/禁用 App Widgets?

java - 唯一地将 Android 应用程序连接到 PC 上的 Java 小程序

kotlin - 在使用 Android 数据绑定(bind)和 Kotlin 的自定义 BindingAdapter 中使用 lambda

java - 如何在 WebView 中保存和加载 HTML 图片?

c# - 使用列表 IndexOf 时未设置 Combobox SelectedIndex