4

Now I have a ViewPager and ViewPagerIndicator to paging my screen on Android.

I override getCount() from FragmentStatePagerAdapter and return 1000 to have 1000 pages. I need to do some code to get the page title base on calendar (dd/MM/yyyy). Any time I scroll, I see that all of 1000 page titles is rebuilt (I print a log at Adapter#getPageTitle(int) ).

This make my pager scroll very slow, not smooth any more.

I think the ViewPagerIndicator shouldn't rebuild all page titles when I scroll 1 page.

UPDATE: add the adapter's source code

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

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


    @Override
    public Fragment getItem(int position) {
        Log.d("xskt", "Adapter.GetItem.position=" + position);
        Calendar calendar = Utilities.selectedProvince.getLastDay(Utilities.selectedCalendar, Utilities.pagerSize - position - 1);
        ResultView resultView = new ResultView(Utilities.selectedProvince, calendar);
        resultView.setTitle(calendar.get(Calendar.DAY_OF_MONTH) + "/" + (calendar.get(Calendar.MONTH) + 1));
        return resultView;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Calendar calendar;

                //DO SOME CALCULATE WITH CALENDAR

        // String title = calendar.get(Calendar.DAY_OF_MONTH) + "/" + (calendar.get(Calendar.MONTH) + 1);
        // return title;
        Log.d("xskt","get page title");
        return ((ResultView) getItem(position)).getTitle();
    }

}
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165

1 Answers1

1

I needed 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, then if the position changes, I just get the difference between the current position and the new position and modify the date to display accordingly.

public class RateFragmentPagerAdapter extends FragmentStatePagerAdapter{

private final int todayPosition;
private RateFragment currentFragment;
private final Calendar todayDate;

/** Constructor of the class */
public RateFragmentPagerAdapter(FragmentManager fm, int today_position, Calendar today_date) {
    super(fm);
    todayPosition = today_position;
    todayDate = (Calendar) today_date.clone();
}

/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {     
    currentFragment = new RateFragment();
    Bundle data = new Bundle();

    data.putInt("current_page", arg0);
    currentFragment.setArguments(data);
    return currentFragment;
}

/** Returns the number of pages */
@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());
}   

}

It is fast because I never call the fragment object, I just compare the current position (final) and the new one in the function.

Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85
  • @Yoann Hercouet I want to know what is in RateDayApplication, Or How can I return date at getPageTitle – Parth Sharma Mar 02 '14 at 17:14
  • I changed my code, but I used this class to keep some constant variables throughout the app. getPageTitle was specific for my need, it depends on what you have in mind. – Yoann Hercouet Mar 04 '14 at 07:23