Skip to content

Instantly share code, notes, and snippets.

@seventhmoon
Last active August 29, 2015 14:23
Show Gist options
  • Save seventhmoon/5e6831a64a0fe3f97109 to your computer and use it in GitHub Desktop.
Save seventhmoon/5e6831a64a0fe3f97109 to your computer and use it in GitHub Desktop.
LoopingViewPager
// Insert 2 more element to the first and the last of the list of the ViewPager
// e.g. from {A, B, C} to {C, A, B, C, A}, connecting the first to the last
//implement ViewPager.SimpleOnPageChangeListener(), override the following method
public void onPageScrollStateChanged(int state) {
if (state == ViewPager2.SCROLL_STATE_IDLE) {
int curr = mViewPager.getCurrentItem();
int lastReal = mViewPager.getAdapter().getCount() - 2;
if (curr == 0) {
mViewPager.setCurrentItem(lastReal, false);
} else if (curr > lastReal) {
mViewPager.setCurrentItem(1, false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment