4

I have ViewPager that contains two Fragments [ Fragment 1] [Fragment 2]. Now based on a selection in [Fragment 1] a new fragment will be added To ViewPager and it will look like this

[Fragment 1] [NewAddedFragment] [Fragment 2]

and also based on the user selection in the first Fragment, I may remove the NewAddedFragment from ViewPager. Then it will look like

[ Fragment 1] [Fragment 2]

Trinimon
  • 13,839
  • 9
  • 44
  • 60
user4o01
  • 2,688
  • 4
  • 39
  • 54
  • Perhaps this can help you - http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view – jaibatrik Apr 09 '13 at 14:36
  • actually no , the link about updating the views inside fragment in viewpager – user4o01 Apr 09 '13 at 14:51
  • I meant adding a fragment to the `PagerAdapter` and then calling `notifyDataSetChanged()` on the `ViewPager` so that the `ViewPager` reloads the components. – jaibatrik Apr 10 '13 at 06:40
  • i did that but an exception was thrown , Fragment "Fragment2" can't change it tag !!! – user4o01 Apr 10 '13 at 07:04

1 Answers1

-1

Look at your 'Adapter' that implements 'FragmentPagerAdapter' and that something will contain a collection type field like an ArrayList that backs the adapter.

Depending on the implementation of the dataStructure, you can alter the underlying collection by calling the corresponding add, set, insert methods on the fragmentpageradapter.

Then your next call to get a new page in the ViewPager should reflect the change to the underlying collection. If it doesn't you may have to call a 'commit' on the FragmentManager, but i do not think thats needed in this instance.

public class BookPagerAdapter extends FragmentPagerAdapter { private int currentPage = 0; private ArrayList mFragments;

public BookPagerAdapter(FragmentManager fm) {
    super(fm);
    mFragments = new ArrayList<Fragment>();
    mFragments.add(new PageFragment(BitmapFactory.decodeResource(getResources(),
               R.drawable.icon_construction32), "TitlePage")); 


    }

example of changing the collection:

bpa.mFragments.add(new PageFragment(map));
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43