setAdapter in a seperate Thread, cause a problem
Hi, I use your ViewPager to make a slidehow using images that i download from my server using AsyncTask then set the adapter in onPostExecute(). When i set the adapter in the AsyncTask Thread the images doesn't show and i have to swipe twice or thrice so the images appear. When i use the adapter in the main thread the problem doesn't exist. By the way i tried also with java Thread and i get the same problem.
This snapshot is from logcat when i set the adapter in the AsyncTask

And this is the one when i set the adapter in the main thread.

I'm using 4 pages in the ViewPager, as you can see in the first snapshot the InfinitePageAdapter instantiate the items then destroy them and that is why they don't show.
In the end i want to thank you for this great project, this what i needed, best regards.
I'm having the same issue. If an Intent is launched from InfiniteViewPagerActivity, fragment 0 and 1 are intantiated two times and then finally destroyed an on rendered. Does it exist a workaround? Many thanks in advance for your time and for this project!!
I had the same problem today. Debugging I noticed that this occur because the ViewPager destroy the items that isn't in the range of pages that should be retained to either side of the current page. If you set ViewPager.setOffscreenPageLimit(4) for example, it will retain 9 itens: the current, four from the left and four from the right. Assuming that we are using the default OffscrenPageLimit that is 1, somehow (I couldn't understand why) when the ViewPager adapter is set from another thread that is not the main, I think that it assumes that the first and the second ins't in the range of the last item and then destroy the first and second ones.
I managed it to work extending InfinitePagerAdapter and doing this trick:
public class MyInfinitePageAdapter extends com.antonyt.infiniteviewpager.InfinitePagerAdapter {
private boolean lastItemInstantiated = false;
public MyInfinitePageAdapter(PagerAdapter adapter) {
super(adapter);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int virtualPosition = position % this.getRealCount();
if(virtualPosition == getRealCount()-1) {
// if it is the last item that is being instantiated
lastItemInstantiated = true;
}else if (virtualPosition == 2 || virtualPosition == getRealCount()-2){
// if the item that is being instantiated is off-range, then set it as false.
lastItemInstantiated = false;
}
return super.instantiateItem(container, position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
int virtualPosition = position % getRealCount();
if(lastItemInstantiated && (virtualPosition == 0 || virtualPosition == 1)){
// if last item is instantiated and the item that is being destroyed
// is the two first ones, then don't destroy it. (this is the trick).
return;
} else if (virtualPosition == getRealCount()-1) {
lastItemInstantiated = false;
}
super.destroyItem(container, position, object);
}
}
and then set the adapter:
viewPager.setAdapter(new MyInfinitePagerAdapter(adapter));
EDIT: Another workaround is to set adapter twice:
viewPager.setAdapter(new InfinitePagerAdapter(adapter));
viewPager.setAdapter(new InfinitePagerAdapter(adapter));
I'm still studying the problem, I hope to come with a real solution soon.