在PageKeyedDataSource中设置DataSource大小,获得占位符效果

Set DataSource size in PageKeyedDataSource to get placeholder effect

我正在使用来自Android架构组件分页库。我正在尝试通过服务器加载数据,没有任何本地数据库。

我的 DataSource class 扩展 PageKeyedDataSource.

下面是我的Paging.Config定制,

PagedList.Config pageConfig = (new PagedList.Config.Builder())
            .setPageSize(20)
            .setInitialLoadSizeHint(30)
            .setPrefetchDistance(5)
            .setEnablePlaceholders(true)
            .build();

我启用了占位符,这让我可以管理 null PagedListAdapterclass。我已经完成了类似下面的操作,

@Override
public void onBindViewHolder(@NonNull MyEventViewHolder holder, int position) {
    Article mArticle = getItem(position);

    if (mArticle != null) {
        holder.txtTitle.setText(mArticle.getTitle());
        holder.txtAuthor.setText(mArticle.getAuthor());
        holder.txtDesc.setText(mArticle.getDescription());
    } else {
        holder.txtTitle.setText("...");
        holder.txtAuthor.setText("......");
        holder.txtDesc.setText(".........");
    }
}

在下一次 API 调用之前,我看不到列表末尾的占位符。

我的问题是,有没有一种方法可以在第一次 API 调用 之后 指定列表 的大小?因为我的 API 正在返回查询中预期的项目总数。如果不可能,我还能做些什么来查看列表的占位符。

注意 : 我无法切换到 ItemKeyedDataSourcePositionalDataSource 因为我的 API 设置为按页响应。

为此,我认为您需要在

中执行 loadInitial
callback.onResult(mArticles, 0, SIZE_OF_TOTAL_ITEMS, null, 2L);

loadBefore

callback.onResult(mArticles, params.key - 1);

loadAfter

callback.onResult(mArticles, params.key + 1);

还有 SIZE_OF_TOTAL_ITEMS 必须在初始加载时知道。