如何在NavigationView中将项目添加到菜单组 [英] How to add an item to a menu group in NavigationView

查看:95
本文介绍了如何在NavigationView中将项目添加到菜单组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的文字游戏中,我目前有一个由 left_drawer_menu.xml 膨胀的硬编码菜单,由3组组成(我的回合,对手的回合以及最后的其他东西):

In a word game for Android I currently have a hardcoded menu inflated from left_drawer_menu.xml and consisting of 3 groups (my turn, opponent turn and finally other stuff):

mLeftDrawer = (NavigationView) findViewById(R.id.left_drawer);
mLeftDrawer.setNavigationItemSelectedListener(
        new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(final MenuItem menuItem) {
                Menu menu = mLeftDrawer.getMenu();

                if (menuItem.getGroupId() == R.id.my_move) {
                    menu.setGroupCheckable(R.id.my_move, true, true);
                    menu.setGroupCheckable(R.id.his_move, false, false);
                    menu.setGroupCheckable(R.id.extras, false, false);
                } else if (menuItem.getGroupId() == R.id.his_move) {
                    menu.setGroupCheckable(R.id.my_move, false, false);
                    menu.setGroupCheckable(R.id.his_move, true, true);
                    menu.setGroupCheckable(R.id.extras, false, false);
                } else if (menuItem.getGroupId() == R.id.extras) {
                    menu.setGroupCheckable(R.id.my_move, false, false);
                    menu.setGroupCheckable(R.id.his_move, false, false);
                    menu.setGroupCheckable(R.id.extras, true, true);
                }

                menuItem.setChecked(true);
                mLeftItem = menuItem.getItemId();
                mDrawerLayout.closeDrawer(mLeftDrawer);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mLeftItem == R.id.start) {
                            startNewGame();
                        } 
                    }
                },DRAWER_CLOSE_DELAY);

                return true;
            }
        });

现在,我正在尝试动态更改该菜单.

Now I am trying to change that menu dynamically.

我有一个包含所有游戏数据的SQLite实例,并使用 IntentService 读取/写入数据库-该部分工作正常.

I have SQLite instance containing all game data and use IntentService to read/write the database - that part works fine.

我当前的困难是:使用以下代码,将新项目添加到 R.id.my_move 组的外部:

My current difficulty is: with the following code, the new items are added outside the R.id.my_move group:

if (mLeftItem == R.id.start) {
    startNewGame();

    Random r = new Random();
    int i = r.nextInt(100);
    menu.add(R.id.my_move, i, i, "Item " + i);   // why is my_move ignored?
} 

更新:

作为进一步的测试,我尝试使用此代码将偶数和偶数项分配给2个单独的组:

As a further test I have tried assigning even and not even items to 2 separate groups with this code:

Random r = new Random();
int i = r.nextInt(100);
int group = 1 + (i % 2); // can be 1 or 2
menu.add(group, i, i, "Item " + i);

但是结果看起来很混乱:

However the result looks chaotic:

我还发现了(可能已经修复了?)问题176300 ,对此感到奇怪也许应该更好地使用子菜单而不是菜单组?

Also I have discovered the (probably already fixed?) Issue 176300 and wonder if maybe sub-menus should be better used instead of menu groups?

推荐答案

在检查因此,您应该在xml中定义排序(对一组中的项目赋予相同的顺序,并在随后的每个组中递增)

So you should define ordering in your xml (give same order to items in one group and increment in each following group)

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

    <group android:id="@+id/my_move" android:checkableBehavior="single">
        <item
            android:orderInCategory="0"
            android:id="@+id/game1"
            android:icon="@drawable/ic_stars_black_24dp"
            android:title="Game #1" />
        <item
            android:orderInCategory="0"
            android:id="@+id/game2"
            android:icon="@drawable/ic_stars_black_24dp"
            android:title="Game #2" />
    </group>
    <group android:id="@+id/his_move" android:checkableBehavior="single">
        <item
            android:orderInCategory="1"
            android:id="@+id/game5"
            android:icon="@drawable/ic_clock_black_24dp"
            android:title="Game #5" />
        <item
            android:orderInCategory="1"
            android:id="@+id/game6"
            android:icon="@drawable/ic_clock_black_24dp"
            android:title="Game #6" />
        <item
            android:orderInCategory="1"
            android:id="@+id/game7"
            android:icon="@drawable/ic_clock_black_24dp"
            android:title="Game #7" />
    </group>
    .....

</menu>

,并在将代码添加到代码中时给出适当的订单值.因此,如果您想在第一组的末尾添加项目,请将其添加为:

and give an appropriate order value while adding the item in your code. So if you want to add the item at the end of first group, add it as:

menu.add(R.id.my_move, Menu.NONE, 0, "Item1");

,如果要添加到第二组,则将其添加为:

and if you want to add to second group, add it as:

menu.add(R.id.his_move, Menu.NONE, 1, "Item2");

您的代码存在的问题可能是xml中的所有项目都具有默认的orderInCategory 0,因此新项目会在之后添加所有这些项目.

The problem with your code could be that all items in the xml have default orderInCategory 0 and so the new item gets added after all these items.

更新

要添加图标,请使用 setIcon MenuItem

To add icon use setIcon method for MenuItem

menu.add(R.id.my_move, Menu.NONE, 0, "Item1").setIcon(R.drawable.ic_stars_black_24dp);

这篇关于如何在NavigationView中将项目添加到菜单组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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