3

I am using a popular solution to circular ViewPager, that is, add two more view in front and last. But it does not slide smoothly between first and last. For example, when we slide right from the last to the first, the scrolling will be cut down once our finger leaves the screen, and the first picture shows immediately.
The reason I find out is that the callback method "onPageSelected" in interface "OnPageChangeListener" was called before the scrolling finished. This is not acceptable, why the new page was selected before the old one vanish? And now, I have no way to solve this problem. Hope your help!
Following is my code fragment:

@Override
public void onPageSelected(int position) {
    if (position == views.size() - 1) {
        viewPager.setCurrentItem(1, false);
    }
    else if (position == 0) {
        viewPager.setCurrentItem(views.size() - 2, false);
    }
}

Other code may be unneccessary, if you had encountered this kind of question, you will be clear about it.
First time asking a question for me. If anything inappropriate, please tell me, thanks!

jinge
  • 785
  • 1
  • 7
  • 20
  • Follow this question http://stackoverflow.com/questions/34481219/circular-viewpager-fragments-dont-work-as-they-supposed-to-after-first-round. Works perfect. – Zeeshan Shabbir Jan 05 '16 at 13:53
  • also this answer fix smoothing scrolling. havn't tried this yet but his solution looks good for smooth scrolling http://stackoverflow.com/a/25522741/5275639 – Zeeshan Shabbir Jan 05 '16 at 13:55
  • Thanks a lot. I tried the solution in that question, but it didn't help a lot. But fortunately I find a way to sovle this problem, I will write below. – jinge Jan 06 '16 at 03:06

1 Answers1

0

I was inspired by the answer of @tobi_b. My solution is when it's time to jump from the fake last to the real first, wait until last scroll finished. Here is my code, it's very simple,

private final class MyPageChangeListener implements OnPageChangeListener {

    private int currentPosition;

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            if (currentPosition == viewPager.getAdapter().getCount() - 1) {
                viewPager.setCurrentItem(1, false);
            }
            else if (currentPosition == 0) {
                viewPager.setCurrentItem(viewPager.getAdapter().getCount() - 2, false);
            }
        }
    }

    @Override
    public void onPageScrolled(int scrolledPosition, float percent, int pixels) {
        //empty
    }

    @Override
    public void onPageSelected(int position) {
        currentPosition = position;
    }

}

However, this solution is not perfect. It has a little flaw when slide fast from the last to the first. If we slide twice in a very short time, the second slide will invalidate. This problem need to be solved.

Community
  • 1
  • 1
jinge
  • 785
  • 1
  • 7
  • 20