Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PositionalDataSource and recyclerview. Why scroll after invalidate()?

Tags:

java

  • Here my fragment
  • Here is my DataSource
  • For test every 10s I call invalidate() (for test). Datasource not changes. My recycleview scroll one page every 10s.

Why?

Log D/DATA: load initial 0 30 30

after invalidate(). Why load start from 8 position after invalidate?

D/DATA: load initial 8 30 30
D/SAME: 0 equal 8 false
    29 equal 37 false
    0 equal 9 false
    1 equal 8 false
    28 equal 37 false
    29 equal 36 false
D/SAME: 0 equal 10 false
....
                RoomSourceFactory sourceFactory = new RoomSourceFactory(context);

        PagedList.Config config = new PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPageSize(10)
                .build();

        LiveData<PagedList<RoomItem>> pagedListLiveData = new LivePagedListBuilder<>(sourceFactory, config)
                .setFetchExecutor(Executors.newSingleThreadExecutor())
                .build();

        roomListPagedAdapter = new RoomListPagedAdapter(
                new DiffUtil.ItemCallback<RoomItem>() {
                    @Override
                    public boolean areItemsTheSame(@NonNull RoomItem oldRoomItem, @NonNull RoomItem newRoomItem) {
                        Log.d("SAME", oldRoomItem.name + " equal " + newRoomItem.name + " " + oldRoomItem.name.equals(newRoomItem.name));
                        return oldRoomItem.name.equals(newRoomItem.name);
                    }

                    @Override
                    public boolean areContentsTheSame(@NonNull RoomItem oldRoomItem, @NonNull RoomItem newRoomItem) {
                        Log.d("CONTENT", oldRoomItem.name + " equal " + newRoomItem.name + " " + oldRoomItem.name.equals(newRoomItem.name));
                        return oldRoomItem.name.equals(newRoomItem.name);
                    }
                },
                onClickListener);

        pagedListLiveData.observe(this, new Observer<PagedList<RoomItem>>() {


            @Override
            public void onChanged(@Nullable PagedList<RoomItem> roomItemPagedList) {
                roomListPagedAdapter.submitList(roomItemPagedList);
            }
        });

        RecyclerView recyclerView = rootView.findViewById(R.id.community_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        recyclerView.setAdapter(roomListPagedAdapter);

public class RoomPositionalDataSource extends PositionalDataSource<RoomItem> {

private Context context;
private Database database;

public RoomPositionalDataSource(Context context) {
    database = new Database(context);

    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            invalidate();
        }
    }, 10000);

}

@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<RoomItem> callback) {

    int start = params.requestedStartPosition;
    int size = params.requestedLoadSize;
    List<RoomItem> result = new ArrayList<>();
    for (int i=start; i < start + size; i++) {
        RoomItem roomItem = new RoomItem();
        roomItem.id = String.valueOf(i);
        roomItem.name = String.valueOf(i);
        result.add(roomItem);
    }

    callback.onResult(result, params.requestedStartPosition);

    Log.d("DATA", "load initial " + params.requestedStartPosition + " " + params.requestedLoadSize + " " + result.size());
}

@Override
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<RoomItem> callback) {
    List<RoomItem> result = new ArrayList<>();
    for (int i=params.startPosition; i < params.startPosition + params.loadSize; i++) {
        RoomItem roomItem = new RoomItem();
        roomItem.id = String.valueOf(i);
        roomItem.name = String.valueOf(i);
        result.add(roomItem);
    }

    callback.onResult(result);

    Log.d("DATA", "load range " + params.startPosition + " " + params.loadSize + " " + result.size());

}

}

Image

like image 412
Игорь Бурков Avatar asked Nov 08 '18 08:11

Игорь Бурков


1 Answers

For anyone coming to this question from search — there is a bug in paging library, more info can be found at Google issue tracker: https://issuetracker.google.com/issues/123834703

like image 141
ratsam Avatar answered Nov 14 '22 23:11

ratsam