I use ViewPager.setCurrentItem()
to automatically swipe to the next page every few seconds. I'd like to disable this as soon as the user starts swiping himself. As far as I can tell, OnPageChangedListener
gets triggered in the same way whether the swipe came from the user or not. It seems like beginFakeDrag()
could help, but it requires to drag by a specified number of pixels, which isn't practical.
You save the current page of the ViewPager in OnPageSelected() and compare it to the position parameter of OnPageScrolled() . If the current page is less than or equal to the position , you are scrolling to the right, if not, you are scrolling to the left.
ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .
ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.
-Finally, you can disable it: view_pager. disableScroll(true); or enable it: view_pager. disableScroll(false);
Are you familiar with SCROLL_STATE_DRAGGING? It indicates that the pager is currently being dragged by the user.
Example
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
// User has dragged
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With