android - NavigationView 项目选择未在选项卡更改时更新

标签 android navigationview

我有一个带有 NavigationView 和两个选项卡 ab 的应用程序。 NavigationView 中的两个 MenuItem 对应于选项卡 ab 因此,当选择选项卡 a 时,NavigationView MenuItem A 应该被选中,对于 MenuItem B 也是如此。选择 MenuItem AB 也应该相应地将选项卡更改为 ab。此处选择意味着使用 选择器 更改图标和文本的颜色,如下所示:

colors_navigationview.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item   android:state_checked="true"
            android:color="@color/red"
            android:drawable="@color/red" />

    <item   android:state_checked="false"
            android:color="@color/gray"
            android:drawable="@color/gray" />

</selector>

布局中的 NavigationView 定义:

<android.support.design.widget.NavigationView
    android:id="@+id/my_navigationview"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/my_navigationview_header"
    app:menu="@menu/my_navigationview"
    app:itemIconTint="@drawable/colors_navigationview"
    app:itemTextColor="@drawable/colors_navigationview"
    />

MenuItems 包含在 SubMenu 中,定义如下:

我的_navigationview.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_menu"
        android:title="@string/foo">
        <menu>
            <item
                android:id="@+id/my_navigationview_tab1"
                android:icon="@drawable/fooicon"
                android:title="@string/bar"/>
            <item
                android:id="@+id/my_navigationview_tab2"
                android:icon="@drawable/baricon"
                android:title="@string/barg"/>
        </menu>
    </item>
</menu> 

问题是,当选择选项卡 ab 时,无论是直接单击选项卡还是在 ViewPager 中滑动选项卡,MenuItems AB 的颜色更新。直接在 NavigationView 中选择 MenuItems 是可行的,并且完全相同的代码是叫。因此,我对为什么这不起作用感到有点困惑。

总结:

  1. 在 NavigationView 中选择一个 MenuItem 会设置正确的颜色并更改选项卡。一切正常。
  2. 选择一个选项卡会更改选项卡,但选择颜色不会改变。我认为 MenuItem 已设置为选中状态,因为调用了 MenuItem.setSelected(true)

我可以尝试如何解决这个问题?我目前不知所措——在我看来,这应该是一件容易的事。我尝试了以下以及更多:

  1. 来自 this 的各种建议问题。
  2. 使各种 GUI 元素失效,还有 NavigationView 和 DrawerLayout。

我的Activity中的相关方法

@Override
public void onCreate()
{
    // ...
    // mTabLayout and mViewPager are created properly.
    mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager)
        {
            @Override
            public void onTabSelected(TabLayout.Tab tab)
            {
                super.onTabSelected(tab);
                updateNavigationViewSelection();
            }
        });
}

@Override
public void onResume()
{
    // ...

    // To set correct NavigationView when resuming. Actually works ok at startup.
    updateNavigationViewSelection();
}

public void updateNavigationViewSelection()
{
    int currentItem = mViewPager.getCurrentItem();
    selectNavigationMenuItem(currentItem);
}

public void selectNavigationMenuItem(int tab)
{
    MenuItem menuItem = null;
    NavigationView navigationView = (NavigationView) findViewById(R.id.my_navigationview);
    switch (tab)
    {
        case TAB1:
            menuItem = getNavigationMenuItemTab1();
            break;

        case TAB2:
            menuItem = getNavigationMenuItemTab2();
            break;
    }

    if (menuItem != null)
    {
        unselectAllNavigationMenuItems(); // Calls setChecked(false) on all MenuItems, may be commented out for testing.

        // We arrive here, from onTabSelected(), onResume() and onNavigationItemSelected().
        // onResume() sets the color correctly, as does onNavigationItemSelected(),
        // but *not* when calling from onTabSelected(). All the values seem to be correct, but nothing happens.
        // It seems that checked is set to true, but there is some invalidation missing.
        // Invalidating NavigationView or DrawerLayout does nothing.
        menuItem.setChecked(true);
    }
}

@Override
public boolean onNavigationItemSelected(MenuItem menuItem)
{
    switch (menuItem.getItemId())
    {
        case R.id.my_navigationview_tab1:
            selectNavigationMenuItem(TAB1);
            // Close drawer.
            return true;

        case R.id.my_navigationview_tab2:
            selectNavigationMenuItem(TAB2);
            // Close drawer.
            return true;

        default:
            return false;

    }
}

@Nullable
private MenuItem getNavigationMenuItemTab1()
{
    MenuItem navigationMenu = getNavigationMenu();
    return navigationMenu == null ? null : navigationMenu.getSubMenu().findItem(R.id.my_navigationview_tab1);
}

@Nullable
private MenuItem getNavigationMenuItemTab2()
{
    MenuItem navigationMenu = getNavigationMenu();
    return navigationMenu == null ? null : navigationMenu.getSubMenu().findItem(R.id.my_navigationview_tab2);
}

@Nullable
private MenuItem getNavigationMenu()
{
    NavigationView navigationView = (NavigationView) findViewById(R.id.my_navigationview);
    return navigationView == null ? null : navigationView.getMenu().findItem(R.id.navigation_menu);
}

private void unselectAllNavigationMenuItems()
{
    MenuItem item;
    item = getNavigationMenuItemTab1();
    if (item != null)
        item.setChecked(false);
    item = getNavigationMenuItemTab2();
    if (item != null)
        item.setChecked(false);
}

最佳答案

看来我找到了一些解决方法。

类似的几个方法:

navigationView.getMenu().getItem(indx).setChecked(true);
navigationView.getMenu().findItem(someId).setChecked(true);
navigationView.setCheckedItem(someId);
navigationView.getMenu().performIdentifierAction(someId, 2);

没用。但是如果你通过调用导航监听器来触发事件

onNavigationItemSelected(MenuItem)

它的工作原理。

例如在您的应用中:

onNavigationItemSelected(getNavigationMenuItemTab1());

关于android - NavigationView 项目选择未在选项卡更改时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37145539/

相关文章:

java - Ionic 构建错误 : Could not create service of type ScriptPluginFactory using BuildScopeServices. createScriptPluginFactory()

android - 通话时播放歌曲

android - 导航 View 的 itemTextColor 的颜色选择器

android - 如何为 NavigationView 中的单个菜单项提供自定义文本颜色?

android - MenuItem 在我的设备上没有显示链式 react

swift - 如何根据 SwiftUI 中的 @State 更改导航 View 的导航 View 样式?

android - 如果我不使用 MediaExtractor,在 Media Codec 中使用哪个 presentationTimeUs?

java - HttpURLConnection 和获取 ASPX 页面

android - 在 Eclipse 中使用 JNI 构建 OpenCV 应用程序

android - 如何收听长按导航 View 项目?