ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.
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.
A viewpager with touch and key event handling disabled by default. Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.
You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .
I just had this same scenario. My solution was to consume the touch event when there are less pages displaying than required to fill the viewpager
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
requestDisallowInterceptTouchEvent(true); // not sure if this is required
PagerAdapter adapter = viewPager.getAdapter();
// consume the move event if we have only one page full - removes flickering artifact
// getNumberOfPagesOnScreen() is a mehtod we have to get the number of pages we are going to display. ymmv
if (adapter.getCount() <= adapter.getNumberOfPagesOnScreen() && event.getAction() == MotionEvent.ACTION_MOVE) {
return true;
} else {
return false;
}
}
});
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