user1549672
user1549672

Reputation: 486

Dynamically adding/removing pages from FragmentStatePagerAdapter

I was looking at the example of the FragmentStatePagerAdapter at http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html

public static class MyAdapter extends FragmentStatePagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        return ArrayListFragment.newInstance(position);
    }
}

I've also looked around at other posts in stackoverflow, but I am still unsure as to how to add/remove pages from the fragmentStatePagerAdapter, and also how exactly the getItem method is called. So if I were looking to add a method to MyAdapter to add pages, how would that be done? Or is that not the standard way of adding pages? Any information is appreciated.

Upvotes: 1

Views: 1467

Answers (1)

marmor
marmor

Reputation: 28179

After adding the new item you'll need to call myAdapter.notifyDataSetChanged();

Also, it seems you use a const for the number or items NUM_ITEMS, you'll need to change that to something dynamic you can change.

Upvotes: 1

Related Questions