I need the same thing, here is the code of what I did, basically I calculate the title to display based on the position of the new fragment.
By default, the fragment corresponding to the current day is displayed, and then, if the position changes, I just get the difference between the current position and the new position and change the date to display accordingly.
public class RateFragmentPagerAdapter extends FragmentStatePagerAdapter{ private final int todayPosition; private RateFragment currentFragment; private final Calendar todayDate; public RateFragmentPagerAdapter(FragmentManager fm, int today_position, Calendar today_date) { super(fm); todayPosition = today_position; todayDate = (Calendar) today_date.clone(); } @Override public Fragment getItem(int arg0) { currentFragment = new RateFragment(); Bundle data = new Bundle(); data.putInt("current_page", arg0); currentFragment.setArguments(data); return currentFragment; } @Override public int getCount() { return RateDayApplication.numberOfDays; } @Override public CharSequence getPageTitle(int position) { Calendar newDate = (Calendar) todayDate.clone(); int diffDays = position - todayPosition; newDate.add(Calendar.DATE, diffDays); return RateDayApplication.dateTitleFormat.format(newDate.getTime()); }
}
This is fast because I never call the fragment object, I just compare the current position (final) and the new one in the function.
source share