如何检测导航抽屉外部的触摸事件 [英] How to detect the touch event outside the navigation drawer

查看:75
本文介绍了如何检测导航抽屉外部的触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序中实现了Android导航抽屉.当用户触摸导航抽屉的外侧时,我可以打开/关闭抽屉.当用户在导航抽屉中触摸/单击时,您中的任何人都可以帮助我检测触摸/单击事件吗?我需要在该事件中执行一些功能. 请检查附件的屏幕截图. 任何帮助都将得到申请.

I have implemented Android Navigation drawer in my application. I am able to open/close the drawer when user touches the out side of navigation drawer. Can any one of you help me in detect the touch/click event when user touch/click out side the navigation drawer. I need to perform some functionality in that event. Please check the attached screenshot. Any help would be appriciated.

推荐答案

您必须使用dispatchTouchEvent()方法处理触摸位置.在此处

You have to handle the touch position in dispatchTouchEvent() method. Check more about touch hierarchy here

@Override    
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        if (mDrawerLayout.isDrawerOpen(mRightDrawerListView)) {

            View content = findViewById(R.id.right_drawer);
            int[] contentLocation = new int[2];
            content.getLocationOnScreen(contentLocation);
            Rect rect = new Rect(contentLocation[0],
                contentLocation[1],
                contentLocation[0] + content.getWidth(),
                contentLocation[1] + content.getHeight());

            View toolbarView = findViewById(R.id.toolbar);
            int[] toolbarLocation = new int[2];
            toolbarView.getLocationOnScreen(toolbarLocation);
            Rect toolbarViewRect = new Rect(toolbarLocation[0],
                toolbarLocation[1],
                toolbarLocation[0] + toolbarView.getWidth(),
                toolbarLocation[1] + toolbarView.getHeight());


            if (!(rect.contains((int) event.getX(), (int) event.getY())) && !toolbarViewRect.contains((int) event.getX(), (int) event.getY())) {
                isOutSideClicked = true;
            } else {
                isOutSideClicked = false;
            }

        } else {
            return super.dispatchTouchEvent(event);
        }
    } else if (event.getAction() == MotionEvent.ACTION_DOWN && isOutSideClicked) {
        isOutSideClicked = false;
        return super.dispatchTouchEvent(event);
    } else if (event.getAction() == MotionEvent.ACTION_MOVE && isOutSideClicked) {
        return super.dispatchTouchEvent(event);
    }

    if (isOutSideClicked) {
        //make http call/db request
        Toast.makeText(this, "Hello..", Toast.LENGTH_SHORT).show();
    }
    return super.dispatchTouchEvent(event);
}

这篇关于如何检测导航抽屉外部的触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆