如何检测导航抽屉外部的触摸事件

5
我在我的应用程序中实现了Android导航抽屉。当用户触摸导航抽屉外部时,我能够打开/关闭抽屉。你们中的任何人能帮助我检测用户在导航抽屉外部触摸/点击事件吗?我需要在该事件中执行一些功能。 请查看附加的截图。enter image description here 任何帮助将不胜感激。

在我的情况下也遇到了同样的问题,找到任何解决方案了吗?因为在我的情况下,下面的可接受答案不起作用。 - Gaurav Mandlik
3个回答

8

您需要在dispatchTouchEvent()方法中处理触摸位置。更多关于触摸层级的信息,请点击此处

@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);
}

1
你可以花一些时间来检查这个。

DrawerLayout.DrawerListener

我希望它能像对我一样对你有所帮助。
一个例子:
        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) {

            }
        });

祝好运。


0

你可以使用onDrawerClosed

当你点击屏幕外面,导航抽屉关闭后,onDrawerClosed将被调用。

public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            //do  here
}

1
谢谢你的回答。onDrawerClosed(View view)将在许多情况下调用。这对我不起作用。 - Ganesh
也许这篇文章可以帮助你:https://dev59.com/NGEh5IYBdhLWcg3wTBxc - Kishore Jethava

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接