Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.