在PageKeyedDataSource中设置数据源大小以获得占位符效果。

3
我正在使用 Android Architecture Component 中的分页库(Paging Library)。我试图通过服务器加载数据,而不需要任何本地数据库。
我的 DataSource 类继承了 PageKeyedDataSource。
以下是我的 Paging.Config 自定义内容:
PagedList.Config pageConfig = (new PagedList.Config.Builder())
            .setPageSize(20)
            .setInitialLoadSizeHint(30)
            .setPrefetchDistance(5)
            .setEnablePlaceholders(true)
            .build();

我已启用占位符,这使得我可以管理PagedListAdapter类中的null。我已经做了以下操作:

@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返回了查询结果中预期的所有项目的总数。如果不可能,我还能做些什么来看到列表的占位符。
注意:我不能切换到ItemKeyedDataSource或PositionalDataSource,因为我的API设置为按页面响应。
1个回答

1
为此,我认为您需要在内部执行loadInitial操作。
callback.onResult(mArticles, 0, SIZE_OF_TOTAL_ITEMS, null, 2L);

加载前

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

loadAfter

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接