Aayush Taneja
Aayush Taneja

Reputation: 561

How to disable swiping a ViewPager from its edges?

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.

Upvotes: 2

Views: 988

Answers (2)

Karim Abdell Salam
Karim Abdell Salam

Reputation: 706

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;
} }

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

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
        }
    }
 });

Upvotes: 2

Related Questions