The problem: I have a ViewPager setup with a FragmentStatePagerAdapter that uses a global arraylist for the contents of its fragments. When I want to update this global array, I simply call the arraylist.add()
method, instantiate a new FragmentStatePagerAdapter and a new ViewPager. However, for some reason, the Adapter isn't calling getItem at all. Not that it's data set is empty (it isn't) or anything... it simply isn't being called. I even set a log to test it. Here's my code:
// Create the adapter that will return a fragment for event
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
/**
* A FragmentStatePagerAdapter that returns a fragment corresponding to
* an index of the global events array
*/
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
/**
* Called to instantiate the fragment for the given page
*/
@Override
public Fragment getItem(int position) {
Log.v("Miles", "getItem called");
// getItem is called to instantiate the fragment for the given page.
return EventFragment.newInstance(events.get(position));
}
/**
* Total number of pages (fragments) there are
* Given by size of the global events array minus 1
*/
@Override
public int getCount() {
return events.size() - 1;
}
}
Are there any problems I could have with this setup? I can verify that the global arraylist has data inside of it, but no fragment is being instantiated to reflect that.
notifyDataSetChanged
on the existing adapter.getItem
is only called when the ViewPager needs the next page. It will not be called right away when you change the adapter data. – OratorionotifyDataSetChanged
? – Oratorio