How to prevent closing Navigation drawer by touch outside the drawer

后端 未结 3 1711
深忆病人
深忆病人 2020-12-20 19:58

I have an Activity with Navigation Drawer. if user device is table and orientation is landscape - I not need to close drawer by click on item in drawer:

if (         


        
3条回答
  •  清酒与你
    2020-12-20 20:34

    Based on the other answer which I wrote here. I have modified the code to suit your question. Please check.

    Check more about touch hierarchy here

    dispatchTouchEvent() method should be overridden in Activity class

    @Override    
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (isDrawerOpen()) { //Your code here to check whether drawer is open or not. 
    
                View content = findViewById(R.id.drawer); //drawer view id
                int[] contentLocation = new int[2];
                content.getLocationOnScreen(contentLocation);
                Rect rect = new Rect(contentLocation[0],
                    contentLocation[1],
                    contentLocation[0] + content.getWidth(),
                    contentLocation[1] + content.getHeight());
    
                if (!(rect.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) {
            return true; //restrict the touch event here
        }else{
            return super.dispatchTouchEvent(event);
        }
    }
    

    Note: As mentioned in the question comments, this is against of Android guidelines. So try to avoid it until unless it is mandatory.

提交回复
热议问题