【问题标题】:Set DataSource size in PageKeyedDataSource to get placeholder effect在 PageKeyedDataSource 中设置 DataSource 大小以获得占位符效果
【发布时间】:2019-01-02 20:42:09
【问题描述】:

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

我的DataSource 类扩展了PageKeyedDataSource

下面是我的Paging.Config自定义,

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

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

@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 设置为基于页面响应。

【问题讨论】:

    标签: android android-layout android-recyclerview android-jetpack android-paging


    【解决方案1】:

    为此我认为你需要在里面做 loadInitial

    callback.onResult(mArticles, 0, SIZE_OF_TOTAL_ITEMS, null, 2L);
    

    加载前

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

    加载后

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 2023-01-23
      • 2014-03-15
      • 2015-11-17
      相关资源
      最近更新 更多