ViewPagerIndicator is not smooth with 1000 pages

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

I override getCount() from the FragmentStatePagerAdapter and return 1000 to have 1000 pages. I need to make a code to get the base of the page title in the calendar (dd / MM / yyyy). Every time I scroll, I see that all 1000 page titles are rearranged (I print the log in Adapter # getPageTitle (int)).

This will make my pager scroll very slow rather than smooth.

I think that ViewPagerIndicator should not rearrange all page titles when scrolling 1 page.

UPDATE: add adapter 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(); } } 
+4
source share
1 answer

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; /** 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()); } 

}

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

+1
source

All Articles