I have an activity that shows a calendar of a month, and allows users to swipe left and right to previous/next month. I want to use ViewPager for the smooth scrolling effect, but it should allow users to scroll left/right infinitely, so there is no way to set an fixed adapter beforehand.
I am wondering if it is possible to recycle the pages like this: initially set the adapter to include only 3 pages, and show the middle page,
- When user swipes left, recycle the 3rd page to create a new page before the current page, set new adapter, and set index to 1 (middle).
- When user swipes right, recycle the 1st page to create a new page after the current page, set new adapter, and set index to 1 (middle).
Can I do this with setAdapter() and setCurrentIndex()? Will this cause unwanted consequences?
In other support recycling for ViewPager and improve performance, you should use View instead of Fragment. Basically this is
instantiateItem
you are looking for.Check out my sample code here RecyclerPagerAdapter