java.lang.IllegalArgumentException: AsyncPagedListDiffer cannot handle both contiguous and non-contiguous lists

huangapple go评论67阅读模式
英文:

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&lt;Problem&gt; problems) {
            Log.d(TAG, &quot;onViewCreated: adapter size = &quot; + 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&lt;Problem&gt; problems) {
            Log.d(TAG, &quot;onViewCreated: adapter size = &quot; + 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成为平铺的,它必须同时满足以下两个条件:

  1. 必须来自一个DataSource,该DataSource重写了以下方法:
@Override
boolean isContiguous() {
    return false;
}
  1. 必须通过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:

  1. Come from a DataSource which overrides:
@Override
boolean isContiguous() {
    return false;
}
  1. 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 DataSources 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.

huangapple
  • 本文由 发表于 2020年8月4日 03:43:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63235970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定