【问题标题】:How to insert item at a position using android Paging 3如何使用android Paging 3在某个位置插入项目
【发布时间】:2021-09-16 15:15:55
【问题描述】:

我正在使用 Paging 3 库从 API 加载项目。我想将一个项目添加到 recyclerview 中的特定位置。

如果我使用普通适配器,我会使用

mArrayList.add(position, item);
notifyItemInserted(position); 

但我正在使用 PagingDataAdapter 并且我已经尝试过了

val newPostResponse = response?.responseBody
homeAdapter.snapshot().items.toMutableList().add(0, newPostResponse)
homeAdapter.notifyItemInserted(0)

我也试过

val newPostResponse = response?.responseBody
homeAdapter.snapshot().items.toMutableList().add(0, newPostResponse)
homeAdapter.notifyDataSetChanged()

这些都不起作用。有人帮助我知道正确的方法。

【问题讨论】:

    标签: android android-paging-library android-paging-3


    【解决方案1】:

    这似乎与How to update single item using Paging 3 library 重复。

    您必须通过 .invalidate() 循环来维护单一事实来源并使 Paging 了解您的本地更改。这是目前唯一支持的向 Paging 添加项目的方式。

    【讨论】:

      猜你喜欢
      • 2020-11-17
      • 2020-11-20
      • 2022-01-07
      • 1970-01-01
      • 2023-01-18
      • 2023-02-12
      • 1970-01-01
      相关资源
      最近更新 更多