Search code examples
androidandroid-viewpagerandroid-adapter

Viewpager adapter loads position 0 & 1 at the same time at instantiateItem


I have an Arraylist of items that I pass to a ViewPager adapter. I am having trouble processing the data since at the adapter's

public Object instantiateItem(@NonNull ViewGroup container, int position), position 0 and 1 are loaded concurrently before the ViewPager switches to the second page.

Is this typical or am i missing something ?.

The Arraylist is loaded when calling the adapter i.e ,

mPhotoAdapter = new PhotoAdapter(mPhotos, this, this);


Solution

  • Yes, This is typical. ViewPager loads another page even though you have not swiped into the other page. As per documentation ViewPager require a minimum of 1 offscreen pages. That means minimum one extra page will be loaded. You can change offscreen page limit by calling this method in ViewPager instance :

    setOffscreenPageLimit (int limit)