Ivan Seidel Ivan Seidel - 1 year ago 83
Java Question

ViewPager skip views on return leaving blank

I'm making an app with 4 main Views. I want that all of them stay on a LIST menu on top (That's ok for now).

Using Fragments, I have the ability to change the view without actualy going to other activity, but since one of my Views have a ViewPager, it's NOT working at all (When I first load the view, the content is OK, but on the seccond time, views on the ViewPager disapear, and appear only if I go 2 index positions away, and go back) (See picture, shown for the first load time, and seccond)

enter image description here

Note that the ViewPager IS NOT to show de menu's items, it's in fact, part of the Fragment of the current menu item (in this case, "Simula'c~ao")

My question is: Is this a known issue, is there a simple way to fix it, or I must do it in another way? I already searched for this problem on the internet, but nothing worked for me (invalidating, setting up the on

to return

I have looked into the ViewPager class, and everything seems to be OK, I have modified it, so that everytime I render the views, it forces to render everything, but neither this way worked.

I have tried to set visibility to GONE and VISIBLE

I have tried to detach and attach the adapter

Tried to cache the views, and return cached.

One thing that semmed to work: Changing the orientation of the Phone. That worked.

Tried lots of things that I even remember... I'm a little furious with this, because the past 24 hours didn't helped me to solve this "mistery" at all. I hope that some of you can =]

My code:


Answer Source

Well, after long hours trying to discover what the hell was this about, @anup-cowkur guided me to a path that would solve my problem.

At first, I tought that the problem was on the PageView, I downloaded the support library, edited, changed LOTS of things, but the problem was not there.

In fact, the problem is on the FragmentPagerAdapter. He uses a sort of "caching" for each of the ViewPager fragment. Because of that, he wasn't "notified" when I returned to the view, causing the last fragments shown to not "actualy" exist on the parents view (in this case, the main fragment, that holds my others fragments from the ViewPager).

Since they are registered on the FragmentManager, what I did was hack into the FragmentPagerAdapter, implementing this method:


public void clear(ViewGroup container){
    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();

    for(int i = 0; i < getCount(); i++){

        final long itemId = getItemId(i);

        // Do we already have this fragment?
        String name = "android:switcher:" + container.getId() + ":" + itemId;
        Fragment fragment = mFragmentManager.findFragmentByTag(name);

        if(fragment != null){
    mCurTransaction = null;

And then, call it on my "onCreate":


done. All fragments are FORCED to be removed from the FragmentManager, causing the "cache" to disapear.

It's not the best solution, but the only one that worked.

EDIT You can find the hole file here: http://pastebin.com/07adWVrr

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download