How to stop blinking on recycler view with architecture components paging library

后端 未结 5 1178
囚心锁ツ
囚心锁ツ 2021-02-05 08:02

I have a chat-like activity where I am using a RecyclerView with a PagedListAdaper to load a bunch of messages. I\'m using a PositionalDataSource to load the data. The loading i

5条回答
  •  遥遥无期
    2021-02-05 08:50

    PagedListAdapterHelper deprecated in Paging Component so we should use AsyncPagedListDiffer instead of it.

    Code example:

        import android.arch.paging.AsyncPagedListDiffer;
        import android.arch.paging.PagedList;
        import android.arch.paging.PagedListAdapter;
        import android.support.annotation.NonNull;
        import android.support.v7.util.DiffUtil;
        import android.view.LayoutInflater;
        import android.view.ViewGroup;
    
    
        public class PagedItemsArrayAdapter extends PagedListAdapter {
    
                private final AsyncPagedListDiffer mDiffer;
    
                public PagedItemsArrayAdapter() {
                    super(DIFF_CALLBACK);
                    mDiffer = new AsyncPagedListDiffer<>(this, DIFF_CALLBACK);
                    setHasStableIds(true);
                }
    
                @Override
                public long getItemId(int position) {
                   Item item = mDiffer.getItem(position);
                   return item.id;
                }
    
                @Override
                public int getItemCount() {
                    return mDiffer.getItemCount();
                }
    
                @Override
                public void submitList(PagedList pagedList) {
    
                    pagedList.addWeakCallback(pagedList.snapshot(), new PagedList.Callback() {
                        @Override
                        public void onChanged(int position, int count) {
                        }
    
                        @Override
                        public void onInserted(int position, int count) {
                            mDiffer.submitList(pagedList);
                        }
    
                        @Override
                        public void onRemoved(int position, int count) {
    
                        }
                    });
    
                }
    
    
                @Override
                public void onBindViewHolder(@NonNull MyViewHolder viewHolder, int position) {
                       Item item = mDiffer.getItem(position);
                       viewHolder.onBind(item); 
                }
    
                private static DiffUtil.ItemCallback DIFF_CALLBACK = new DiffUtil.ItemCallback() {
                    @Override
                    public boolean areItemsTheSame(@NonNull Item oldItem, @NonNull Item newItem) {
                        return oldItem.getId() == newItem.getId();
                    }
    
                    @Override
                    public boolean areContentsTheSame(@NonNull Item oldItem, @NonNull Item newItem) {
                        return oldItem.equals(newItem);
                    }
                };
    

提交回复
热议问题