简体   繁体   English

每次显示 Android NavigationView 时如何以编程方式禁用和启用项目

[英]How to programatically disable and enable items every time an Android NavigationView is displayed

I am moving some menu items from the options menu to the navigation menu.我正在将一些菜单项从选项菜单移动到导航菜单。 My app uses a NavigationView that is populated by a menu as described at https://developer.android.com/reference/android/support/design/widget/NavigationView.html我的应用程序使用由菜单填充的 NavigationView,如https://developer.android.com/reference/android/support/design/widget/NavigationView.html 所述

One of the items calls webView.goBack() on the WebView in the main activity.其中一项在主活动的WebView上调用webView.goBack() When it was placed in the options menu, it was only enabled if webView.canGoBack() .当它被放置在选项菜单中时,它仅在webView.canGoBack()启用。 Otherwise, it was disabled (grayed out).否则,它被禁用(变灰)。 To accomplish this, onPrepareOptionsMenu() included the command:为此, onPrepareOptionsMenu()包含以下命令:

back.setEnabled(webView.canGoBack());

As onPrepareOptionsMenu() is called every time the options menu is about to be displayed, this would update the status of the menu item to correctly reflect the state of the WebView .由于每次将要显示选项菜单时都会调用onPrepareOptionsMenu() ,这将更新菜单项的状态以正确反映WebView的状态。

However, I have not been able to replicate this behavior with the NavigationView.但是,我无法使用 NavigationView 复制此行为。 Is there a method or class similar to onPrepareOptionsMenu() that is called each time the NavigationView is prepared?是否有类似于onPrepareOptionsMenu()每次准备 NavigationView 时调用的方法或类?

PS.附注。 Other people who have addressed similar questions have always referred to using a ListView, which was an older method of populating a navigation drawer.解决类似问题的其他人总是提到使用 ListView,这是一种填充导航抽屉的旧方法。 This question specifically relates to using a NavigationView with a menu.这个问题特别涉及使用带有菜单的 NavigationView。

The answer to this question is to add a DrawerListener and override onDrawerStateChanged .这个问题的答案是添加一个DrawerListener并覆盖onDrawerStateChanged

// Create the navigation drawer.
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
// The `DrawerTitle` identifies the drawer in accessibility mode.
drawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.navigation_drawer));

// Listen for touches on the navigation menu.
final NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);

// Get handles for `navigationMenu` and the back and forward menu items.  The menu is zero-based, so item 1 and 2 and the second and third items in the menu.
final Menu navigationMenu = navigationView.getMenu();
final MenuItem navigationBackMenuItem = navigationMenu.getItem(1);
final MenuItem navigationForwardMenuItem = navigationMenu.getItem(2);

// The `DrawerListener` allows us to update the Navigation Menu.
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
    }

    @Override
    public void onDrawerOpened(View drawerView) {
    }

    @Override
    public void onDrawerClosed(View drawerView) {
    }

    @Override
    public void onDrawerStateChanged(int newState) {
        // Update the back and forward menu items every time the drawer opens.
        navigationBackMenuItem.setEnabled(webView.canGoBack());
        navigationForwardMenuItem.setEnabled(webView.canGoForward());
    }
});

NavigationView exposes its underlying Menu with getMenu() . NavigationView 使用getMenu()公开其底层 Menu。 You can use that to find menu items and make changes to them.您可以使用它来查找菜单项并对其进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM