0

I have an issue where I need to warn the user before leaving a tab, the tab sits in a ViewPager and is controlled by the tabs in the actionbar.

The viewPager has a typical onPageChangeListener and the actionbar tabs use a TabListener

Basically I am trying to figure out how I can intercept the action to START changing a tab, and only allow it to occur if the user acknowledges the dialog. The callbacks I have tried seem to not block the change to occur. Is there any simple way to achieve this that I am missing? The same action should occur regardless if the user attempts to leave teh tab via either swiping (the ViewPager) or selecting a tab (TabListener)

xceph
  • 1,036
  • 2
  • 13
  • 28
  • Seems in tab listener there shouldn't be any problem, since You need to switch page manually, right? If so, why it mentioned in the question? – sandrstar Dec 18 '13 at 03:28
  • Is this way not http://stackoverflow.com/questions/8134336/disabling-enabling-paging-in-viewpager-in-android applicable? – sandrstar Dec 18 '13 at 03:39

1 Answers1

0

Using the class below you can block swipe.
Try it and if any problem then comment and I will try to solve it.

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);
    }
}
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81