When I call setCurrentItem(int pos) method on ViewPager with endless items, UI gets blocked if the new position is less than current by at least 2. If the new position is less by 1 or greater than the old position the ViewPager works properly. For testing purpose I changed max number to 300, UI was not blocked. It looks like ViewPager iterates through items count to match the new position with them, in my case item count is Integer.MAX_VALUE. The UI block lasts for about 20-30 seconds and then it goes to the needed position correctly. Here is the adapter
public class DateCountVPAdapter extends FragmentStatePagerAdapter {
public DateCountVPAdapter(Date startDate, FragmentManager fm) {
super(fm);
this.startDate = startDate;
calendar = Calendar.getInstance();
calendar.setTime(startDate);
}
private Date startDate;
private Calendar calendar;
@Override
public DateCountVPFragment getItem(int position) {
Calendar currentDate = (Calendar)calendar.clone();
currentDate.add(Calendar.DAY_OF_MONTH, position);
return DateCountVPFragment.newInstance(startDate, currentDate.getTime());
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
}
I also tried to extend ViewPager and to amend setCurrentItem(int pos) method, however there were private methods which made impossible to do so. Also I tried to copy the class, however an issue with PagerAdapter, which had package private method used by ViewPager. Please offer any solutions to avoid UI block. Thanks in advance.