DrawerLayout与webView联合使用时,滑动冲突

问题发生现象:登陆进入地图主界面(webview 会将地图加载出来),然后打开抽屉菜单,然后手在(Textview)标题上,上下左右滑动时,会引起底图,地图页面会跟着滑动。正常情况下,打开抽屉菜单后,手不管怎么滑动,地图都不应该跟着动。

处理方法:关键是去监听 手接触的第一个view OnTouchListener方法,然后调用 requestDisallowInterceptTouchEvent方法去拦截点击滑动响应。

具体方法如下:

View.OnTouchListener myTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                webView.requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                webView.requestDisallowInterceptTouchEvent(false);
                break;
        }
        return true;
    }
};

调用: drawerTitle.setOnTouchListener(myTouchListener);

OnTouchListener 方法返回true与false的区别:默认给定的返回值 false运行,发现只能监听到这个 view 的“按下”事件,“移动”和“抬起”都不能够监听到;如果改为 true,就都监听到。可参考:http://blog.sina.com.cn/s/blog_6dc41baf01010vdd.html

关于requestDisallowInterceptTouchEvent的讲解可参考:https://www.jianshu.com/p/7e92121814ed

猜你喜欢

转载自blog.csdn.net/sunqihui22/article/details/94719756