【问题标题】:Updating a single item in a recylerview. I am using paging library and would like to update a single item/row更新 recylerview 中的单个项目。我正在使用分页库并想更新单个项目/行
【发布时间】:2019-03-08 08:13:13
【问题描述】:

我正在使用提交列表将分页列表值传递给适配器。当我更新单个项目时,请考虑我正在单击 recyclerview 中提要的点赞按钮。如何更新单个项目。

我正在按照这个例子进行分页实现

https://github.com/saquib3705/PagingLibrarySampleApp

它只是加载数据并更新recyclerview。我想为项目添加点赞按钮,并在用户喜欢如何实现它时更新列表。 还要看看这正是我正在寻找的 Update list items in PagingLibrary w/o using Room (Network only)

【问题讨论】:

标签: android android-architecture-components android-jetpack android-mvvm android-paging-library


【解决方案1】:

假设我的猜测是正确的:

  1. 你的视图模型有LiveData<PagedList<Article>>getter
  2. 您有PagedListAdapter 实现并通过submitList 将数据设置到其中
  3. 您在列表和列表项之间共享您的视图模型(这很自然)

然后将其添加到您的适配器构造函数中(当然,Article 只是代表某个实体的示例类,您将拥有自己的):

        super(new DiffUtil.ItemCallback<Article>() {
            @Override
            public boolean areItemsTheSame(@NonNull Article article1, @NonNull Article article2) {
                return article1.getId().equals(article2.getId());
            }

            @Override
            public boolean areContentsTheSame(@NonNull Article article1, @NonNull Article article2) {
                return article1.equals(article2);
            }
        });

...您的问题已自动解决。

【讨论】:

    猜你喜欢
    • 2020-06-01
    • 2017-05-04
    • 2020-11-17
    • 1970-01-01
    • 2021-07-25
    • 2021-09-20
    • 1970-01-01
    相关资源
    最近更新 更多