4

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 getItemPosition to return POSITION_NONE...)

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: https://www.dropbox.com/s/u4mlxr3k6ta9yrm/MainActivity.java https://www.dropbox.com/s/2dqfnzjs2wl89hj/SimulationFragment.java

Views: https://www.dropbox.com/s/d6ruc1zjovqu6ob/activity_main.xml https://www.dropbox.com/s/lp1iea13klr77iq/activity_simulation.xml https://www.dropbox.com/s/1mkl3jqmo7g4wh8/item_simulation.xml

Ivan Seidel
  • 2,394
  • 5
  • 32
  • 49

3 Answers3

5

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:

FragmentPagerAdapter.java

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.detach(fragment);
        }
    }
    mCurTransaction.commitAllowingStateLoss();
    mCurTransaction = null;
}

And then, call it on my "onCreate":

myPagerAdapter.clear(mViewPager);

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

Ivan Seidel
  • 2,394
  • 5
  • 32
  • 49
  • 1
    My god, thank you so much for this. I was slamming my face against the desk after hours of trying to figure this out. You haven't gotten enough credit for this solution. I had to search for your makeFragmentName method. For any future visitors, I'm pretty sure it's this one: http://stackoverflow.com/a/9293207/1354164 (it works for me at least). Please buy yourself a beer and pretend it's from me! – GMBrian Mar 04 '14 at 20:01
  • I'm happy that it could help @GMBrian ! I have just updated the answer, now it doesnt needs the makeFragmentName. Also, added a link for the hole file on pastebin – Ivan Seidel Mar 04 '14 at 21:37
0

You are using a FragmentStatePagerAdapter. This adapter uses fragments inside the ViewPager. You have this ViewPager inside another parent Fragment. So you have Fragments inside a Fragment. Nested fragments are not supported. This is why you are having problems. See: Why it is not possible to use ViewPager within a Fragment? It actually is

Your best bet is to use a normal PagerAdapter instead of a FragmentStatePagerAdapter. This uses regular views instead of Fragments and should work fine.

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
0

If the issue occur when it is necessary to make more than one swipe to obtain get the page on screen, this code solves it:

mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);
Leo Rodríguez
  • 169
  • 2
  • 7