Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
635 views
in Technique[技术] by (71.8m points)

android - Navigation drawer not opening from cutom menu button

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.

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

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

In any of the child activity:

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

Please help..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...