Strange effect with ViewPager sliding Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Strange effect with ViewPager sliding without wasting too much if your time.

The question is published on by Tutorial Guruji team.

As I described in the answer of this my post GridView animated HORIZONTAL sliding I have a custom calendar made of ViewPager and GridViews on each of 3 views of the ViewPager.

I’ve created an “universal” InfinitePagerAdapter extends PagerAdapter. And my CalendarPagerAdapter extends this InfinitePagerAdapter.

I have adapters for each of the 3 GridViews (One on each page of the 3 ViewPager pages)

Currently I have the following strange effect:

  1. I slide right and left (by finger) with and everything is fine: Sliding is just fine The page I slide to (prev/next) is inflated and I see the it just fine during the slide.

But:

  1. In some undefined moment the sliding looks like this: prev/next page is not inflated The page I slide to (prev/next) is not inflated and I see white page during sliding. When the sliding is finished the page is inflated just fine.

Firstable I thought my “pages trick” in the InfiniteViewPagerAdapter is not fast enough for example if slide “too fast” several pages etc.

But the same effect I have if I just put buttons “prev”/”next” and scroll ViewPager by code: ViewPager.setCurrentItem(position, true); Each Slide after this command has the described strange effect (not inflated page). I’ve tried to “force” invalidate all pages on each slide etc. but nothing by now.

Answer

I found an easy solution that is good enough for my task:

In the begining right after setting the adapter I set current item to Integer.MAX_VALUE/2

    pager.setAdapter(adapter);
    pager.setCurrentItem(Integer.MAX_VALUE / 2);

I’ve dropped the idea of reusing the view of the 3 pages kept in memory, so I just 1. Create view in instantiateItem(…) and add it to container:

view = inflateView(.....)
container.add(view)

2. Remove it from container in destroyItem(…) container.remove(view)

We are here to answer your question about Strange effect with ViewPager sliding - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji