导航抽屉未从常规菜单按钮打开 [英] Navigation drawer not opening from cutom menu button

查看:109
本文介绍了导航抽屉未从常规菜单按钮打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android应用程序,那边有一个导航抽屉.由于导航抽屉工具栏不能透明,并且末尾的三个点按钮图标也不能更改,因此我选择隐藏该工具栏,并显示我的自定义布局.它将为我提供所需的所有功能.

I'm working on an Android App, I have a navigation drawer over there. Since the navigation drawer toolbar can't be transparent, and the ending three dots button icon can't be changed, I opted for hiding that toolbar, and show my custom layout. It will give me all the functionality what ever is needed.

但是我现在面临的问题是,一旦活动开始,如果单击自定义菜单按钮,该按钮将无法打开.拖动并打开它之后,每当我单击菜单按钮时,它都会打开导航抽屉.

But the problem I'm facing right now is, once the activity starts, if I click the custom menu button it doesn't open. Once I drag it and open, after that whenever I click the menu button it opens the navigation drawer.

我可能会缺少什么?这就是我正在做的,同时调试它甚至到达了else部分,但没有打开.

What might i be missing? This is what I'm doing, while debugging its even coming to the else part, but doesn't open.

在BaseActivity中:

In BaseActivity:

drawer                  = (DrawerLayout) findViewById(R.id.drawer_layout);
    toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


ivLeft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (drawer.isDrawerOpen(Gravity.LEFT)) {
                drawer.closeDrawer(Gravity.LEFT);
            } else {
                drawer.openDrawer(Gravity.LEFT);
            }
        }
    });

在任何子活动中:

 toolbar.setVisibility(View.GONE);
    navigationView.setVisibility(View.GONE);

请帮助.

推荐答案

问题的根本原因是,您将抽屉View的可见性设置为GONE.但是,您描述的奇怪行为的直接原因是由于DrawerLayout及其辅助类之一如何在抽屉状态改变时更新子View.

The root cause of your problem is the fact that you're setting the drawer View's visibility to GONE. The direct cause of the odd behavior you describe, though, is due to how DrawerLayout and one of its helper classes update the child Views when the drawer state changes.

您设置为打开和关闭抽屉的OnClickListener正常工作.似乎没有,因为抽屉ViewGONE.但是,当您通过拖动手动打开抽屉时,DrawerLayout使用的ViewDragHelper触发了一个回调方法,该方法将抽屉明确设置为VISIBLE.以编程方式打开抽屉(即使用openDrawer()方法)时,不会触发此回调,这说明了为什么仅通过单击自定义切换按钮而不显示抽屉.将抽屉拖动一次后,可以看到抽屉View,然后按预期的方式进行切换.

The OnClickListener you set to open and close the drawer was working as it should. It just didn't appear to be, since the drawer View was GONE. When you manually opened the drawer by dragging, however, the ViewDragHelper that DrawerLayout uses was firing a callback method that explicitly sets the drawer to VISIBLE. This callback is not fired when the drawer is opened programmatically - that is, with the openDrawer() method - which explains why the drawer didn't show just by clicking your custom toggle button. After you had dragged the drawer open once, the drawer View was visible, and the toggle would then work as expected.

默认情况下,抽屉View处于关闭状态,因此您无需隐藏它,只需删除navigationView.setVisibility(View.GONE);行即可.

The drawer View is in its closed state by default, so you don't need to hide it, and you can just remove the navigationView.setVisibility(View.GONE); line.

这篇关于导航抽屉未从常规菜单按钮打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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