How to detect the touch event outside the navigati

2020-06-04 07:01发布

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.enter image description here Any help would be appriciated.

3条回答
迷人小祖宗
2楼-- · 2020-06-04 07:50

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);
}
查看更多
别忘想泡老子
3楼-- · 2020-06-04 07:50

You can use onDrawerClosed

when you touch outside screen onDrawerClosed will call after close of Navigation Drawer

public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            //do  here
}
查看更多
爷、活的狠高调
4楼-- · 2020-06-04 07:55

you may spend some time by checking this out.

DrawerLayout.DrawerListener

I hope it will help you as it helps me.

An example:

        DrawerLayout drawerLayout = activity.findViewById(R.id.drawer_layout);
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View view, float v) {

            }

            @Override
            public void onDrawerOpened(@NonNull View view) {

            }

            @Override
            public void onDrawerClosed(@NonNull View view) {

            }

            @Override
            public void onDrawerStateChanged(int i) {

            }
        });

Good luck.

查看更多
登录 后发表回答