I have a ViewPager and ViewPagerAdapter as defined below:
private static class ViewPagerAdapter extends FragmentPagerAdapter {
@Override
public int getCount() {
return fragments.size();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Within my activity I am populating fragments array which is my list of Fragments which I can view by scrolling horizontally:
// populate fragments
fragments.add(....);
pager_adapter = new ViewPagerAdapter(getSupportFragmentManager());
view_pager.setAdapter(pager_adapter);
All this works fine. But, now I have a situation where in at run time I have to add one more fragment to this list and then refresh the view so that the new fragments becomes visible too upon scrolling. For this I added the following code inside my activity:
public static void addPageAndRefresh(Page page) {
fragments.add(PinFragment.newInstance(fragments.size(), true, page, book, false, false, null));
pager_adapter.notifyDataSetChanged();
}
After this, I do get a new fragment, but it is not the one which I added, its a copy of the last fragment in the last before calling notifyDataSetChanged().
Is this the correct way of doing this? It looks like although the fragments array is updated properly, but the view is not picking up the updated array. But, if it isn't then I shouldn't be seeing the new fragment after scrolling.