'Pagination with Viewpager

I am having a ViewPager and i am getting huge amount of data from API response, so i am looking forward to implement Pagination with my ViewPager items as i could load only 20 items at a time and once the user is at the last item we can show a loader for a bit and can load the next set of data, as my app is using more memory on the device.

I have researched and found Pagination for RecyclerView but couldn't find relevant resources to use with ViewPager. Anybody who have worked on similar kind of requirement can guide me with the steps to achieve the same.



Solution 1:[1]

Try to use View Pager 2 that uses the same adapter as recycler view

Solution 2:[2]

For this you can create custom pagination with viewpager. Suppose you get 10 item first time then put a condition that if viewpager current position is 8 then call another slot of api which will get data 11 to 20 and so on.

Solution 3:[3]

If someone still wants to solve this problem, just use paging 3, if your api supports pagination. Then create PagingAdapter and connect it to ViewPager2.

Paging3 guide: https://developer.android.com/topic/libraries/architecture/paging/v3-overview

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Shawky
Solution 2 Mayur Coceptioni
Solution 3 Ruslan Mamytov