Infinite Auto Scrolling using Android View Pager

Recently , I have implemented a concept of infinite scrolling in android. This is not much difficult task but it’s tricky . I have implemented it by View pager using Paging Adapter .  Anyone can do it by  view pager adapter like FragmentStatePagerAdapter. I have used one more thing which is View Pager Indicator .

                View Pager with Indicators

To implement it  , take android View Pager and  Frame Layout  for indicators in activity XML :

In your Activity , prepare data for view pager and set a suitable adapter in it .You need to set your view pager in LoopingCirclePageIndicator .

Here , One new class is introduced which is LoopingCirclePageIndicator .This class is handling all the possible situations of view pager current page selection using one interface LoopingPagerAdapter .You can refer this code from these links :

1. LoopingCirclePageIndicator

2.  LoopingPagerAdapter

In general , getCount() method of adapter returns object size but  in this case we are going to implement infinite scrolling , that’s why  we will use INTEGER.MAX_VALUE in getCount() method . There is one more method  getRealCount() , which returns real size of your object for view pager indicators.

Actually , we create a new page every time for infinite scrolling in adapter.

Auto Scroll View Pager Implementation

Now let’s discuss most challenging functionality that I enjoyed exploring and implementing on my own  . Here is the code to do the same after setting adapter in view pager.

 

All suggestions are welcome 🙂 .

 

Leave a Reply