使用分页库时,RecyclerView列表刷新不正确

时间:2019-11-01 00:14:02

标签: android android-room android-jetpack android-paging-library

我在项目中使用Paging库。

在“活动”中,观察者观察Room库返回的数据,当Room的数据更改时,UI会更新。

当我滚动到RecyclerView的末尾时,分页库开始将数据加载到Room中。

但是RecyclerView的列表刷新不正确,并滚动到上一个位置。

我的代码怎么了?

enter image description here 活动(观察者)。

PagingItemRepository repository = new PagingItemRepository(FindDatabase.getInstance(PagingListActivity.this).pagingItemDao());

        repository.getAllItems().observe(this, new Observer<PagedList<ItemEntity>>() {
            @Override
            public void onChanged(PagedList<ItemEntity> itemEntities) {
                adapter.submitList(itemEntities);
            }
        });

适配器

class PagingListAdapter extends PagedListAdapter<ItemEntity, ItemHolder> {

        protected PagingListAdapter() {
            super(DIFF_CALLBACK);
        }

        protected PagingListAdapter(@NonNull DiffUtil.ItemCallback<ItemEntity> diffCallback) {
            super(diffCallback);
        }

        @NonNull
        @Override
        public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            return new ItemHolder(LayoutInflater.from(PagingListActivity.this).inflate(R.layout.list_item, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
            ItemEntity entity = getItem(position);
            if (entity != null) {
                String value = entity.getText();
                holder.textView.setText(value);
            }
        }

    }

    class ItemHolder extends RecyclerView.ViewHolder {

        TextView textView;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);

            textView = itemView.findViewById(R.id.text);
        }
    }

DiffUtil

private static DiffUtil.ItemCallback<ItemEntity> DIFF_CALLBACK =
            new DiffUtil.ItemCallback<ItemEntity>() {
                // Concert details may have changed if reloaded from the database,
                // but ID is fixed.
                @Override
                public boolean areItemsTheSame(ItemEntity oldEntity, ItemEntity newEntity) {
                    return oldEntity.getId() == newEntity.getId();
                }

                @Override
                public boolean areContentsTheSame(ItemEntity oldEntity,
                                                  ItemEntity newEntity) {
                    return oldEntity.equals(newEntity);
                }
            };

PagingItemRepository

public class PagingItemRepository {
    private PagingItemDao itemDao;
    LivePagedListBuilder<Integer, ItemEntity> builder;

    public PagingItemRepository(PagingItemDao itemDao) {
        this.itemDao = itemDao;
        DataSource.Factory<Integer, ItemEntity> factory = itemDao.getAllItems();
        PagedList.Config config = new PagedList.Config.Builder().
                setPageSize(60).
                setPrefetchDistance(0).
                setEnablePlaceholders(true).
                setInitialLoadSizeHint(120).
                //setMaxSize(200).
                build();
        builder = new LivePagedListBuilder<>(factory, config);
        builder.setBoundaryCallback(new MyBoundaryCallback(itemDao));
    }

    LiveData<PagedList<ItemEntity>> getAllItems() {
        return builder.build();
    }
}

MyBoundaryCallback

public static class MyBoundaryCallback extends PagedList.BoundaryCallback<ItemEntity> {
        private PagingItemDao itemDao;

        public MyBoundaryCallback(PagingItemDao itemDao) {
            this.itemDao = itemDao;
        }

        @Override
        public void onZeroItemsLoaded() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final List<ItemEntity> entities = new ArrayList<>();
                    for (int i = 0; i < 30; i++) {
                        count++;
                        ItemEntity entity = new ItemEntity();
                        entity.setText(count + "");
                        entities.add(entity);
                    }
                    itemDao.addItems(entities);
                }
            }).start();
        }

        @Override
        public void onItemAtFrontLoaded(@NonNull ItemEntity itemAtFront) {
        }

        @Override
        public void onItemAtEndLoaded(@NonNull ItemEntity itemAtEnd) {
            final Long id = itemAtEnd.getId();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (id >= 530) {
                        return;
                    }

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    final List<ItemEntity> entities = new ArrayList<>();
                    for (int i = 0; i < 50; i++) {
                        count++;
                        ItemEntity entity = new ItemEntity();
                        entity.setText(count + "");
                        entities.add(entity);
                    }


                    itemDao.addItems(entities);
                }
            }).start();
        }
    }

PagingItemDao

@Dao
public interface PagingItemDao {

    @Query("select * from item")
    DataSource.Factory<Integer, ItemEntity> getAllItems();

    @Insert
    void addItems(List<ItemEntity> entities);

    @Query("delete from item")
    void delete();
}

1 个答案:

答案 0 :(得分:0)

最后,我解决了这个问题。 ItemEntity,这是我的Room实体,其id类型以前是Long类型(引用类型),然后我将Long更改为long类型(基本类型),然后一切都很好。