0

There's a lot of questions and solutions on how to block swipes in an ViewPager.

Example: Control which directions the ViewPager can be scrolled, dynamically with UI feedback

However I would like to only block forward swipe on an specific pages (I have a form and register button that takes next page programatically)

Example: Page 1 -> swipe <- Page 2 -> swipe <- Page 3 (form) -> button -> Page 4

Tried to extend class ViewPager with methods onInterceptTouchEvent and onInterceptTouchEvent but when doing so I can't get the button to work because everything is read as MotionEvent.ACTION_MOVE so don't know when to let the event to go thru or when to use return false;

Community
  • 1
  • 1
roady
  • 597
  • 2
  • 6
  • 20

1 Answers1

0


import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent;

public class CustomViewPager extends ViewPager {

private boolean enabled; private boolean blockSwipe = false;

public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); this.enabled = true; }

@Override public boolean onTouchEvent(MotionEvent event) { if (this.enabled) { return super.onTouchEvent(event); }

return false; }

public void setBlockSwipe(boolean blockSwipe) { this.blockSwipe = blockSwipe; }

@Override public boolean onInterceptTouchEvent(MotionEvent event) { if (blockSwipe) return false; else return super.onInterceptTouchEvent(event); }

}

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81