Reloading ViewPager does not create first 3 fragments
I have a SherlockFragment based Fragment that includes a ViewPager working with Fragments via a FragmentStatePagerAdapter. I have implemented an "infinite" rollout, so the adapter returns getCount () 1000. It works. The problem occurs when I replace () the main fragment from the main activity with other data, the onCreateView of that main fragment called during first initialization: I recreate the adapter and set it to the ViewPager, run notifyDataSetChanged () and set the corresponding view using setCurrentItem () ... At the moment I can't see the Fragment as getItem () is not being called. In case I view the ViewPager left or right, the third snippet from the middle is displayed and then everything works, i.e. GetItem () is only called while scrolling:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_timebar, container, false);
mPager = (ViewPager)view.findViewById(R.id.timebar_pager);
mTitleIndicator = (TitlePageIndicator)view.findViewById(R.id.timebar_titles);
mPagerAdapter = new MyAdapter(container.getContext(), getSherlockActivity().getSupportFragmentManager(), mPager);
mPager.setAdapter(mPagerAdapter);
mPagerAdapter.setDate(mStartDate, mPagerAdapter.MIDDLE_POSITION);
return view;
}
public class MyAdapter extends FragmentStatePagerAdapter {
public final int FULL_COUNT = 1000;
private final int MIDDLE_POSITION = FULL_COUNT/2;
private long mMiddleFragmentDate;
private ViewPager mViewPager;
public MyAdapter(Context context, FragmentManager fragmentManager, ViewPager viewPager)
{
super(fragmentManager);
mMiddleFragmentDate = mStartDate;
mViewPager = viewPager;
}
public long getMiddlePosDate()
{
return mMiddleFragmentDate;
}
private void refreshData(int position)
{
Log.d("TIMEBAR","@@@@@ refreshData pos=" + position);
mViewPager.setCurrentItem(position);
notifyDataSetChanged();
View localView = mViewPager.findViewWithTag(Integer.valueOf(position));
if (localView != null)
{
Log.d("TIMEBAR","@@@@@ refreshData localView found & != null");
localView.invalidate();
}
mTitleIndicator.notifyDataSetChanged();
}
public Calendar getDateByPosition(int position)
{
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(mMiddleFragmentDate);
cal.add(Calendar.DAY_OF_YEAR, (position - MIDDLE_POSITION) * mNumOfDaysPerView);
return cal;
}
@Override
public void destroyItem(ViewGroup viewGroup, int position, Object object)
{
super.destroyItem(viewGroup, position, object);
}
@Override
public int getCount()
{
return FULL_COUNT;
}
@Override
public Fragment getItem(int position)
{
GregorianCalendar cal = (GregorianCalendar)getDateByPosition(position);
return TimebarFragment.newInstance(position, cal.getTimeInMillis(), mNumOfDaysPerView);
}
@Override
public int getItemPosition(Object paramObject)
{
return POSITION_NONE;
}
@Override
public CharSequence getPageTitle(int position)
{
Calendar cal = getDateByPosition(position);
StringBuilder sb = new StringBuilder();
sb.append(mCalendarUtils.getDayOfMonth(cal));
sb.append(" ");
if (mNumOfDaysPerView > 1) {
sb.append(mCalendarUtils.getMonthNameShort(cal));
} else {
sb.append(mCalendarUtils.getMonthNameLong(cal));
}
if (mNumOfDaysPerView > 1) {
sb.append(" - ");
cal.add(Calendar.DAY_OF_YEAR, mNumOfDaysPerView - 1);
sb.append(mCalendarUtils.getDayOfMonth(cal));
sb.append(" ");
sb.append(mCalendarUtils.getMonthNameShort(cal));
}
if (calNow.get(Calendar.YEAR) != cal.get(Calendar.YEAR)) {
sb.append(",");
sb.append(cal.get(Calendar.YEAR));
}
return sb.toString();
}
public void setDate(long date, int position)
{
mMiddleFragmentDate = date;
refreshData(position);
}
}
source to share
Finally, using the android-support-v13 library instead of the android-support-v4 library and using getChildFragmentManager () instead of getSupportFragmentManager () solved all these problems. Now:
-
Reloading the main fragment causes the corresponding fragments to be reloaded in the pager (called onCreateView ())
-
It is not possible to instantiate multiple instances of ViewPager at the same time.
source to share