Android Paging 3 Adapter scroll to the target position in the next page from other view

Viewed 536

I have 2 activities: List Item and Detail item.

  • List item using Paging 3 to display list item. When click an item it direct to Detail item in other activity.
  • Detail item using ViewPager to view detail and it can swipe left/right to view next/pre item(item list same with the list in item view).

The problem is:

  • When start to view detail, we send a list item (loaded item page) Ex: 30 item ~ 3 page. But, when swipe to the end list that sent from list, I need load next page to continue display detail. and when back from that next page (ex: item 35 - page 4) to the list item, I have to scroll to the target item (item 35).

Please help me to handle load next page (page 4) and scroll the list to the target item (item 35). I know paging 3 adapter using LiveData so it could not trigger load next page if the list item in the background.

Thanks for any help!

1 Answers

The best way to do this is that both activity observes changes on the same data source.

Because you are using Paging 3 you can use room database as your data source.

With Paging 3 and Room extensions you can observe the changes and implement a remote mediator to load more data.

For a complete implementation and to get you started please refer to the documentation here that fits your case.

Related