简体   繁体   中英

Menu drawer to open only with two fingers swipe for DrawerLayout

How can I disable single finger menu drawer open (swipe left to right) for the DrawerLayout, however allow the menu drawer to open with two fingers swipe?

Single finger swipe to not open the drawer menu, but child views to be able to process touch events. Only the menu open functionality is to be forbidden for single finger swipe.

An update : I have read this very useful topic: ( Android: Difference between onInterceptTouchEvent and dispatchTouchEvent? ) .

So I've decided to overrrite method onInterceptTouchEvent
public boolean onInterceptTouchEvent(MotionEvent arg) { if (arg.getPointerCount() < 2 && !this.isDrawerOpen(this.listView)) { return true; } else { return super.onInterceptTouchEvent(arg); } }

However obviously it does not send touch events to children views. I think I have to use LOCK_MODE_LOCKED_CLOSED in order to control enable/disable of drawer. I will post that solution here later.

I think this is the solution using LOCK_MODE for this task, if anyone has something better, please share:

@Override
public boolean onInterceptTouchEvent(MotionEvent arg) {
    if (arg.getPointerCount() < 2) {
        if (!this.isDrawerOpen(this.listView)) {
            // The drawer is locked closed. The user may not open it.
            this.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        }
    } else {
        // The drawer is unlocked.
        this.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }

    return super.onInterceptTouchEvent(arg);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM