1

I'm having a hard time understanding how to replace fragments using the FragmentPagerAdapter. I have a class that extends FragmentPagerAdapter (android.support.v13.app.FragmentPagerAdapter) and my MainActivity implements ActionBar.TabListener, in the FragmentPageAdapter class I use the getItem() to setup my 3 fragments. so far so good.

One of these fragments is a ListView (pos #1), which I use a Listener to check on the onItemClick().

I want to replace the current ListView Fragment with another fragment so what I did is that inside the onItemClick() I perform the following code:

FragmentManager mFragmentManager = mActivity.getFragmentManager();          

//new fragment to replace
Fragment mFragment = new TabPlaceFragment();

mFragmentManager.beginTransaction()
                            .replace(R.id.pager, mFragment)
                            .addToBackStack(null)
                            .commit();

What this is doing is that the fragment is replace with a blank fragment, I'm assuming is the pager, but the mFragment does not get replace. I tried giving an Id to the Current fragment and tried replacing it.

mFragmentManager.beginTransaction()
                                .replace(R.id.frament_old, mFragment)
                                .addToBackStack(null)
                                .commit();

But what this does is that it overlaps the new fragment on top of the old fragment

finally I tried getting the Id of the fragment to replace with the new, but this also gave me a blank fragment.

//this gives me the current fragment I want to replace
    Fragment mCurrent = mFragmentManager.findFragmentByTag(MainHelper.getFragmentName(mViewPager.getId(), 1));


    mFragmentManager.beginTransaction()
                                    .replace(mCurrent.getId(), mFragment)
                                    .addToBackStack(null)
                                    .commit();

I'm sure I'm not implementing the method correctly but I cannot find a good example to follow the logic of my code. I thought of putting the fragment replacement in the MainActivity under getItem(), but don't know how to call it from within my onItemClick() Listener.

Any help is highly appreciated!!!

Thanks.

The Heist
  • 1,374
  • 1
  • 15
  • 31
kimosavi
  • 31
  • 4
  • after `commit` call `viewpager.getAdapter().notifayDataSetChanged();` – mmlooloo Aug 29 '14 at 07:10
  • Thank you @mmlooloo, it does make perfect sense that the `mViewPager` needs to notify of a change made, sadly in my code it didn't work, I still got the two fragment overlapping each other. In the past I've worked with `FrameLayout` in the xml and replament works great, right now I'm not (I'm following example from Google and the code generated by Eclipse). I will play around with the `FrameLayout` but I think I need to understand better how the `ViewPager` works and interacts with the `FragmentManager`. – kimosavi Aug 29 '14 at 15:45
  • i suggest you:http://stackoverflow.com/questions/18745600/how-to-replace-fragment-inside-viewpager-using-pageradapter and http://stackoverflow.com/questions/7445437/replace-fragment-with-another-fragment-inside-viewpager and http://stackoverflow.com/questions/18588944/replace-one-fragment-with-another-in-viewpager and http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager – mmlooloo Aug 29 '14 at 16:11
  • Thanks again @mmlooloo! the example in [link](http://stackoverflow.com/questions/18745600/how-to-replace-fragment-inside-viewpager-using-pageradapter) did help understand, as expected I need a `FrameLayout` to host my fragments, which I wasn't doing. I will give it a try. – kimosavi Aug 29 '14 at 16:33
  • also `POSITION_UNCHANHED` and `POSITION_NONE` – mmlooloo Aug 29 '14 at 16:34
  • Thanks @mmlooloo! the sample code you referenced worked great! I understand it now how to replace fragments within the `FragmentPagerAdapter`. – kimosavi Aug 29 '14 at 22:56
  • for me was following this example: [link](https://github.com/danilao/fragments-viewpager-example) I was missing a `FrameLayout` to host the new Fragments. – kimosavi Aug 31 '14 at 02:23

0 Answers0