2

I have an activity containing a Navigation Drawer and a AutoScrollViewPager. When I **swipe from the left edge of my phone's screen to open the Navigation Drawer, the AutoScrollViewPager's page changes instead of Navigation Drawer coming out. How can I disable the swiping of AutoScrollViewPager from its edges?

P.S. I tried adding margin to the AutoScrollViewPager but it looks ugly and does not work if the margin isn't large enough.

Aayush Taneja
  • 511
  • 7
  • 18

2 Answers2

2

Use Edge Flags

Here is the Documentation

Try using below code:

final View pagerView = findViewById(R.id.Pager); 
pagerView.setOnTouchListener(new View.OnTouchListener() 
{         
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
       if (event.getAction() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() == MotionEvent.EDGE_LEFT) {
            return true;   //disable swipe
        }
    }
 });
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Thanks for the answer but It is not working for me. I have put a toast inside the IF block and found out that the control never goes inside the IF block. Also, a warning is being displayed that "it has setOnTouchListener called but it does not override performOnClick". Also, I would like to mention that the ViewPager is an AutoScrollViewPager and hope that it does not make a difference. @rafsanahmad007 – Aayush Taneja Nov 17 '17 at 17:24
  • i can't exactly tell why it is not working. but u should take a look at this:https://stackoverflow.com/questions/6645537/how-to-detect-the-swipe-left-or-right-in-android and https://stackoverflow.com/questions/12884250/motionevent-handling-in-scrollview-in-android?noredirect=1&lq=1 – rafsanahmad007 Nov 17 '17 at 18:44
0

Try using below code:

public class CustomViewPager extends ViewPager {

private boolean enabled;

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

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

    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.enabled) {
        if (event.getAction() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() == MotionEvent.EDGE_LEFT) {
            return true;   //disable swipe
        }
    }

    return false;
}

public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
} }
Karim Abdell Salam
  • 706
  • 1
  • 6
  • 15