I created an example project to show an issue I am been struggling with using RecyclerView, Room and Paging, which is that the RecyclerView is unexpectedly scrolling down on data updating.
https://github.com/HappyPeng2x/RoomRecyclerViewExample
The app has a Room database, and I am using an adapter derived from PagedListAdapter to display its values in a RecyclerView.
The query is observed as shown by the code below so that every update of the table will be reflected by the adapter.
PagedList.Config plConfig =
new PagedList.Config.Builder().setEnablePlaceholders(false)
.setPrefetchDistance(10)
.setPageSize(20).build();
new LivePagedListBuilder<>
(mDB.getMyDao().getAllPaged(), plConfig)
.build()
.observe(this, new Observer<PagedList<MyEntry>>() {
@Override
public void onChanged(PagedList<MyEntry> myList) {
adapter.submitList(myList);
}
});
For testing I populate the table with 1 000 key/value pairs. The keys start at 1 and end at 1 000, and the values are all initiated as INITIAL.
I include in each displayed element a toggle button; clicking on it will toggle the value from INITIAL to FINAL and reverse.
When pressing the toggle button at the 155th element, the displayed value changes from INITIAL to FINAL without any issue.
When doing the same operation at the 243rd element, pressing the button causes the RecyclerView to scroll down, which is not expected.
The issue repeats itself each time a button is pressed around this position.
I took a video capture so that the issue can be observed.
I have been struggling quite a bit with this issue, and feel a bit ashamed because it seems like a basic use of Architecture Components, so I would be really happy to get any help.