英文:
java.lang.IllegalArgumentException: AsyncPagedListDiffer cannot handle both contiguous and non-contiguous lists
问题
I'm using android jetpack paging library. and I have the following observer on my MVVM.
observer = new Observer<PagedList<Problem>>() {
@Override
public void onChanged(PagedList<Problem> problems) {
Log.d(TAG, "onViewCreated: adapter size = " + problems.size());
adapter.submitList(problems);
}
};
now I'm retrieving some data from the database and I want to submit a List<Problems> to the adapter.
I did some research and found a way of converting a List of Object to a PagedList.
The problem is that whenever I try to submit a new list I get the following Error message :
java.lang.IllegalArgumentException: AsyncPagedListDiffer cannot handle both contiguous and non-contiguous lists.
so, is there's any way that I could change my PagedList into a Contiguous PagedList? I think the answer is to make my PagedList a PagedList with a single Page. any help please?.
英文:
I'm using android jetpack paging library. and I have the following observer on my MVVM.
observer = new Observer<PagedList<Problem>>() {
@Override
public void onChanged(PagedList<Problem> problems) {
Log.d(TAG, "onViewCreated: adapter size = " + problems.size());
adapter.submitList(problems);
}
};
now I'm retrieving some data from the database and I want to submit a List<Problems> to the adapter.
I did some research and found a way of converting a List of Object to a PagedList.
The problem is that whenever I try to submit a new list I get the following Error message :
java.lang.IllegalArgumentException: AsyncPagedListDiffer cannot handle both contiguous and non-contiguous lists.
so, is there's any way that I could change my PagedList into a Conteguos PagedList ? I think the answer is to make my PagedList a PagedList with a single Page. any help please?.
答案1
得分: 1
在Paging2中,PagedList
可以是连续的或平铺的,以处理用户可能会滚动到占位符很远的情况,而不是加载许多相邻页面,您希望能够“跳转”到该位置并从那里继续加载。
因此,为了使PagedList
成为平铺的,它必须同时满足以下两个条件:
- 必须来自一个
DataSource
,该DataSource
重写了以下方法:
@Override
boolean isContiguous() {
return false;
}
- 必须通过
PagedList.Config.enablePlaceholders
启用占位符,因为这样做基本上是选择了平铺行为(如果支持的话)。
你看到的错误基本上是一个警告,提示你正在尝试支持快速滚动/跳转情况,但使用的DataSource
只支持加载相邻页面。
如果你还没有尝试过,我建议尝试使用Paging3,因为它取消了整个平铺的概念,将跳转的概念推广到所有类型的DataSource
,通过将它们推广到一个明确选择并定义跳转行为的单一PagingSource
类型。
另外,考虑到你暗示你正在从数据库中获取数据以提交为静态列表 - 通过像Room或SqlDelite这样的工具,通常的做法是使用它们内置的Paging支持来直接生成一个DataSource.Factory实现。
英文:
In Paging2, a PagedList
is either contiguous or tiled, to handle cases where a user may scroll far into placeholders and instead of loading many pages adjacently, you want to be able to "jump" to that position and resume loading from there.
So in order for a PagedList
to be tiled, it must both:
- Come from a
DataSource
which overrides:
@Override
boolean isContiguous() {
return false;
}
- Placeholders must be enabled via
PagedList.Config.enablePlaceholders
as doing so basically opts you into tiling behavior (if it is supported).
The error you're seeing is basically a warning that you're trying to support the fast-scroll / jumping case, but using a DataSource
which only supports loading adjacent pages.
If you haven't checked it out yet, I would recommend trying out Paging3, as it does away with the entire tiling thing and generalizes the concept of jumps to all types of DataSource
s by generalizing them to a single PagingSource
type that explicitly opts in and defines jumping behavior.
Also, since you hinted that you're fetching data from DB to submit as a static list - the common way to do this via something like Room or SqlDelite would be to use their built-in Paging support to generate a DataSource.Factory implementation directly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论