Android, disable the swiping to interact with SlidingPaneLayout

后端 未结 5 2104
無奈伤痛
無奈伤痛 2020-12-28 18:57

I need to disable swiping to open/close SlidingPaneLayout because my main view is a map. I\'ll open/close it using a button.

5条回答
  •  灰色年华
    2020-12-28 19:46

    Building on Roman Zhilichs answer with two improvements.

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return !swipeEnabled || super.onInterceptTouchEvent(ev);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!swipeEnabled) {
            if (!getChildAt(0).dispatchTouchEvent(ev)) {
                ev.offsetLocation(-getChildAt(0).getWidth(),0);
                getChildAt(1).dispatchTouchEvent(ev);
            }
            return true;
        }
        return super.onTouchEvent(ev);
    }
    

    First, this tries to dispatch the MotionEvent to the first child before dispatching it to the second. This way, both child views stay interactive. Second, MotionsEvents dispatched to the second child view must be offset by the width of the first view. This assumes that the sliding pane slides in from the left, as is usual with SlidingPaneLayout.

提交回复
热议问题