Android Paging Library 如何知道加载更多数据? [英] How does Android Paging Library know to load more data?

查看:24
本文介绍了Android Paging Library 如何知道加载更多数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对开发 Android 应用还很陌生,我正在努力以正确的方式"完成所有事情.所以现在,我正在我的项目中实现新的 Android Paging Library,我需要从网络服务器加载文章列表.

Iʼm fairly new to developing Android apps and Iʼm trying to do everything "the right way." So right now, Iʼm implementing the new Android Paging Library into my project, where I need to load a list of articles from a network server.

我有一个 ArticlesRepository 类,它返回一个 ArticleList 类,其中包含我想在 RecyclerView 中显示的 ArticleListItem 的实例.文章列表已在服务器上分页,因此存储库发送对第一页的请求并返回一个 ArticleList,其中 page 属性设置为 1articles 属性,其中包含请求页面上文章的 List.我不知道一页可以有多少篇文章.

I have an ArticlesRepository class that returns an ArticleList class containing instances of ArticleListItem that I would like to display in a RecyclerView. The list of articles is paginated already on the server, so the repository sends a request for the first page and returns an ArticleList with the page property set to 1 and the articles property containing a List<ArticleListItem> of articles on the requested page. I donʼt know how many articles can be on one page.

现在,我能够实现一个 PageKeyedDataSource,但它只获取第一页:

Now, I was able to implement a PageKeyedDataSource<Integer, ArticleListItem>, but it only fetches the first page:

@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, ArticleListItem> callback) {
    ArticleList list = load(1);
    if (list != null) {
        callback.onResult(list.articles, null, next(list));
    }
}

@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, ArticleListItem> callback) {
    ArticleList list = load(previous(params.key));
    if (list != null) {
        callback.onResult(list.articles, previous(list));
    }
}

@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, ArticleListItem> callback) {
    ArticleList list = load(next(params.key));
    if (list != null) {
        callback.onResult(list.articles, next(list));
    }
}

previous/next 函数返回一个 Integer 和上一个/下一个页码或 null 如果有不是一个.

The previous/next functions return an Integer with the previous/next page number or null if there isnʼt one.

在我的 ViewModel 中,我像这样配置 PagedList:

In my ViewModel, I configure the PagedList like this:

PagedList.Config config = new PagedList.Config.Builder()
            .setEnablePlaceholders(false)
            .setInitialLoadSizeHint(1)
            .setPageSize(1)
            .setPrefetchDistance(1)
            .build();

这样我就可以加载第一页,但是当我滚动到 RecyclerView 的底部(在 NestedScrollView 中)时,什么也没有发生.调试显示没有调用PageKeyedDataSource.loadAfter方法.

This way Iʼm able to load the first page, but when I scroll to the bottom of the RecyclerView (that is inside a NestedScrollView), nothing happens. Debugging shows that the PageKeyedDataSource.loadAfter method is not invoked.

我是否必须以某种方式告诉 PagedList 必须加载下一页,还是 RecyclerView/DataSource/GodKnowsWhatElse 的工作而我只是做错了什么?感谢您的建议.

Do I have to somehow tell the PagedList that the next page has to be loaded, or is it the RecyclerView/DataSource/GodKnowsWhatElseʼs job and Iʼm just doing something wrong? Thanks for any advice.

推荐答案

分页库应该自动知道何时加载新项目.您实现中的问题是分页 RecyclerView 位于 NestedScrollView 内,根据此 issue 库没有内置支持.

The paging library should know automatically when to load new items. The problem in your implementation is that the paged RecyclerView is inside a NestedScrollView and according to this issue the libary doesn't have built in support for that.

当你把 recyclerview 放在一个无限滚动的父级中时,它会布局其所有子项,因为父项提供了无限尺寸.

when you put recyclerview inside an infinite scrolling parent, it will layout all of its children because the parent provides infinite dimensions.

您需要创建自己的嵌套滚动视图实现,在这个gist中实际上有一个也许能帮到你.

You'll need to create your own implementation of Nested Scroll View, there is actually one here in this gist that might be able to help you.

还建议在这个自定义嵌套滚动视图中添加fillViewPort:

It is also suggested to add fillViewPort to this custom nested scroll view:

android:fillViewport="true" 到可滚动容器

android:fillViewport="true" to scrollable container

这篇关于Android Paging Library 如何知道加载更多数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆