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

查看:820
本文介绍了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 包含 ArticleListItem 实例的类,我想在RecyclerView中显示。文章列表已经在服务器上进行了分页,因此存储库发送第一页的请求并返回带有 ArticleList c $ c>属性设置为 1 文章属性包含列表< ArticleListItem> 所请求页面上的文章。我不知道在一个页面上可以有多少篇文章。

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< Integer,ArticleListItem> ,但它只获取第一页:

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内,并根据此发布 libary没有内置的支持。

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.

你需要创建自己的嵌套滚动实现看来,这个要点中有一个可以帮助你。

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天全站免登陆