In my app, I have a ViewPager. In one of the pages, I have another ViewPager, in which I have disabled the swipe gestures through a custom ViewPager implementation:
public class NonSwipeableViewPager extends ViewPager {
public NonSwipeableViewPager(Context context) {
super(context);
}
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
}
This works well; the swipe gestures in the child ViewPager are disabled. However, the parent ViewPager cannot be swiped unless the swipe starts from the very edge of the screen. How can I make the child ViewPager ignore all touch events/pass them on to the parent view?
The answer turned out to be very simple!
public class NonSwipeableViewPager extends ViewPager {
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean canScrollHorizontally(int direction) {
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