0

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.

Hayk
  • 31
  • 1
  • 3

1 Answers1

1

By default, setCurrentItem(int pos) implicitly calls that the view transition should be animated as a smooth scroll if the ViewPager has already been through its first layout with the current adapter. So some of your time locked may be from the ViewPager attempting to draw smooth scrolling between your screens, especially if it has to create Fragments in the transition.

Your first step should be using setCurrentItem(int pos, boolean smoothScroll) unless you really care about the smooth scrolling.

Bryan Dormaier
  • 800
  • 6
  • 10
  • Thanks for your support, but it is not related to the transition, I have already tried it. It actually hangs in `populate(int newCurrentItem)` method of `ViewPager` in the following loop: `for (int pos = mCurItem + 1; pos < N; pos++) {}` where `N` is `Integer .MAX_VALUE`. I guess I will use `RecyclerView` instead... It would be better if there was a checking in the `ViewPager` whether new position is less than the old one, in order to use reversed loop. – Hayk Jun 15 '18 at 06:48
  • If you must use ViewPager, I'd recommend going with a smaller value. You could go much smaller than Integer.MAX_VALUE and still have what would appear to the end user to be endless scrolling. I had a similar instance recently where it was a carousel on a number of items and did collection size * 1000. It was much smaller than the max int, which led to the time needed to walk through quite a bit smaller. In the case of a calendar, assuming you start at MaxInt/2, you have almost 3 million years in each direction. – Bryan Dormaier Jun 15 '18 at 16:51