android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.42k stars 8.29k forks source link

Paging Library Scroll with AdapterDataObserver #726

Closed GitHubHQ closed 4 years ago

GitHubHQ commented 5 years ago

Currently I am observing a room database with the paging library and using this code to scroll when a new item is inserted at the top

RecyclerView.AdapterDataObserver() {
    @Override
    public void onItemRangeInserted(int positionStart, int itemCount) {
        if (positionStart == 0 && itemCount == 1) {
            layoutManager.scrollToPositionWithOffset(0, 0);
       }
    }
});

which works fine normally. However, when scrolling to a page further down, positionStart is no longer 0 but some higher number (what I assume to be the "top" of what's loaded) when inserting to the front of the list. Is this the intended behavior and is there a way around it?

dlam commented 4 years ago

Do you have placeholders enabled?

Either way - APIs to handle loadState (which you can filter for PREPEND loads), are now available in Paging3: PagingDataAdapter.loadStateFlow / .addLoadStateListener()