11

我正在尝试使用新的Paging LibraryRoom作为数据库,但我遇到了一个问题,PagedList数据库返回的列表不应该与发送到 UI 的列表相同,我map在向用户显示之前和在此map操作期间有一些实体我更改了列表大小(添加项目),显然Paging Library不支持这种操作,因为当我尝试运行应用程序时出现此异常:

Caused by: java.lang.IllegalStateException: Invalid Function 'function_name' changed return size. This is not supported.

查看分页库源代码,您会看到此方法:

static <A, B> List<B> convert(Function<List<A>, List<B>> function, List<A> source) {
    List<B> dest = function.apply(source);
    if (dest.size() != source.size()) {
        throw new IllegalStateException("Invalid Function " + function
            + " changed return size. This is not supported.");
    }
    return dest;
}

当您在使用它之前添加动态项目时,是否有解决方法或需要处理的东西PagedList

这就是我正在做的

@Query("SELECT * FROM table_name")
fun getItems(): DataSource.Factory<Int, Item>

本地资源

fun getItems(): DataSource.Factory<Int, Item> {
    return database.dao().getItems()
        .mapByPage { map(it) } // This map operation changes the list size
}
4

2 回答 2

0

我面临同样的问题,仍在寻找更好的解决方案。
就我而言,我必须在每个用户从 API加载之前显示1 个部分,这是我的解决方法。

class UsersViewModel : ViewModel() {
    var items: LiveData<PagedList<RecyclerItem>>

    init {
        ...
        items = LivePagedListBuilder<Long, RecyclerItem>(
            sourceFactory.mapByPage { it -> mapUsersToRecyclerItem(it) }, config).build()
    }

    private fun mapUsersToRecyclerItem(users: MutableList<User>): List<RecyclerItem> {
        val numberOfSection = 1
        for (i in 0 until numberOfSection) {
            users.add(0, User()) // workaround, add empty user here
        }

        val newList = arrayListOf<RecyclerItem>()
        newList.add(SectionItem())
        for (i in numberOfSection until users.size) {
            val user = users[i]
            newList.add(UserItem(user.login, user.avatarUrl))
        }
        return newList
    }
}

我当前的用户类

data class User(
    @SerializedName("login")
    val login: String,
    @SerializedName("id")
    val id: Long = 0,
    @SerializedName("avatar_url")
    val avatarUrl: String
) {
    constructor() : this("", 0, "")
}

当然,要显示Section,我将有另一种方法而不将其添加到RecyclerView data list(例如仅使用位置),但在我的情况下,用户可以从列表中删除项目,因此使用位置可能难以处理

实际上,我回滚以使用旧负载更多方式(使用EndlessRecyclerViewScrollListener)但希望它有所帮助

于 2018-09-24T07:35:01.917 回答
0

我想我找到了解决方案..

虽然这是一种解决方法,但它对我有用。

就我而言,我试图为这样的名称创建一个字母分区列表:

**A - HeaderItem**
Aaron - Item
Anny - Item
**B - HeaderItem**
Bob - Item
Bil
**C - HeaderItem**
....

ROOM 中的项目当然只是名称,当我尝试映射分页项目并添加部分标题时,它会更改列表大小并且我得到相同的错误。

我所做的是,HeaderItem 对象包装了这样的 Item:

首先,所有Item都实现了ListItem接口

interface ListItem{
 const val HEADER = 0
 const val ITEM = 1
 fun getItemType() : Int
}

然后标题项看起来像这样

class HeaderItem(val headerTitle : String, val item : Item) : ListItem {
  @override
  fun getItemType() : Int {
    return ListItem.HEADER
  }
}

然后当我映射项目时,当添加一个 HeaderItem 时,它会在其中包含一个项目,这样映射的 PagedList 大小不会改变。现在我没有得到这个例外。

但是,这会产生一些额外的工作,因为我必须在适配器中明确设置 HeaderItem 装饰,当绑定标题项时,我必须注意内部项 + 它的所有逻辑,例如单击侦听器等。

如果支持更改开箱即用的列表大小,我会很高兴。

于 2019-07-04T06:48:02.247 回答