如何使用列表大小与Room数据库返回的列表大小不同的AAC分页库 [英] How to use AAC paging library with list size different than list size returned by Room database
问题描述
我正在尝试使用新的 Paging Library
和 Room
作为数据库,但是我遇到了一个问题,即由数据库不应该是发送到UI的相同列表,我先将一些实体 map
展示给用户,然后在此 map
操作中更改列表的大小(添加项目),显然 Paging Library
不支持这种操作,因为当我尝试运行该应用程序时,出现了以下异常:
I'm trying to use the new Paging Library
and Room
as database but I'm facing a problem, the PagedList
returned by the database is not supposed to be the same list sent to the UI, I map
some entities before showing it to the user and during this map
operation I change the list size (add items), apparently the Paging Library
doesn't support this kind of operation because when I try to run the app I get this exception:
Caused by: java.lang.IllegalStateException: Invalid Function 'function_name' changed return size. This is not supported.
查看分页库源代码,您会看到以下方法:
Looking at the paging library source code you see this method:
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
之前,是否有解决方法或需要处理的事情?
Is there a workaround or something for dealing with when you add dynamic items to the PagedList
before using it?
这就是我正在做的
DAO
@Query("SELECT * FROM table_name")
fun getItems(): DataSource.Factory<Int, Item>
LocalSource
LocalSource
fun getItems(): DataSource.Factory<Int, Item> {
return database.dao().getItems()
.mapByPage { map(it) } // This map operation changes the list size
}
推荐答案
我想我已经找到了解决方案..
I think I've found a solution..
尽管这是一种解决方法,但对我来说却是有效的.
Although it's a workaround it worked for me.
就我而言,我试图为这样的名称创建一个按字母顺序划分的列表:
In my case, I was trying to create an alphabet sectioned list for a names like this:
**A - HeaderItem**
Aaron - Item
Anny - Item
**B - HeaderItem**
Bob - Item
Bil
**C - HeaderItem**
....
ROOM中的项目当然只是名称,当我尝试映射分页的项目并添加节标题时,它会更改列表大小,并且出现相同的错误.
The items in ROOM are only the names of course, when I am trying to map the paged items and add the sections headers it change the list size and I am getting the same error.
我所做的是,HeaderItem对象包装了一个这样的Item:
What I did is, the HeaderItem object wraps an Item like this:
首先,所有Item都实现了ListItem接口
First, all Items are implementing the ListItem interface
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时,它将包含一个Item,这样映射的PagedList大小就不会改变.现在我没有得到这个例外.
Then when I map the items, when adding a HeaderItem, it takes an Item in it, this way the mapped PagedList size doesn't change. Now I am not getting this exception.
但是,这创建了一些额外的工作,因为我必须显式地设置HeaderItem装饰,并且还要在适配器中进行设置,在绑定标题项时,我必须注意内部Item及其所有逻辑,例如单击侦听器等.
However, This creates some additional work as I had to set the HeaderItem decoration explicitly and also in the adapter, when bind the header item I had to take care the internal Item + all its logic such as click listeners etc'.
如果能支持开箱即用的列表大小更改,我会很高兴.
I will be happy if there will be support for changes in the list size out of the box.
这篇关于如何使用列表大小与Room数据库返回的列表大小不同的AAC分页库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!