Updating a single item in a recylerview. I am using paging library and would like to update a single item/row

馋奶兔 提交于 2020-06-25 06:49:30

问题


I am passing pagedlist values to adapter using submit list. when i update a single item consider i am clicking a like button of a feed in a recyclerview. how to update the single item.

i am following this example for paging implementation

https://github.com/saquib3705/PagingLibrarySampleApp

which just loads data and update the recyclerview. I would like to add like button for the items and update the list when user liked how to achieve it. Also look at this which is what i m exactly looking for Update list items in PagingLibrary w/o using Room (Network only)


回答1:


Assuming my guess is correct:

  1. Your view model has LiveData<PagedList<Article>> getter
  2. You have PagedListAdapter implementation and set data into it via submitList
  3. You share your view model between list and list item (which is natural)

Then by adding this into your adapter constructor (naturally Article is just sample class representing some entity, you will have your own) :

        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);
            }
        });

...you have your question solved automatically.



来源:https://stackoverflow.com/questions/55059100/updating-a-single-item-in-a-recylerview-i-am-using-paging-library-and-would-lik

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!