如何在Android NavigationView菜单中对组进行排序/排序?

时间:2018-04-12 04:29:56

标签: java android

互联网充满了对不同问题的答案(如何对组中的项目进行排序?答案是使用OrderInCategory。我的问题是如何对组自己进行排序或排序?

在我的应用中,我必须以编程方式在每个组中添加/删除菜单项,以及一次显示/隐藏整个组。

我设置的东西是这样的:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:id="@+id/nav_menu_stuff1">
    </group>

    <group android:id="@+id/nav_menu_stuff2">
    </group>

    <group android:id="@+id/nav_menu_stuff3">
    </group>

    <group android:id="@+id/nav_menu_stuff4">
    </group>

</menu>

然后在代码中,我添加/删除:

getMenu().add(R.id.nav_menu_stuff1, some_id, 0, some_name);
getMenu().removeItem(some_id);

以及一次显示/隐藏整个群组:

getMenu().setGroupVisible(R.id.nav_menu_stuff1, stuff1_is_visible);
getMenu().setGroupVisible(R.id.nav_menu_stuff2, stuff2_is_visible);
getMenu().setGroupVisible(R.id.nav_menu_stuff3, stuff3_is_visible);
getMenu().setGroupVisible(R.id.nav_menu_stuff4, stuff4_is_visible);

对我来说问题是Android不维护组的顺序。在某些时候,我清除了中间组中的所有项目(例如stuff2),然后重新添加项目。当我这样做时,该组最终位于NavigationView的底部。那不是我想要的地方。

我们如何维持群组的顺序?

2 个答案:

答案 0 :(得分:1)

试试这段代码:

菜单栏xml:

     <?xml version="1.0" encoding="utf-8"?>
     <menu xmlns:android="http://schemas.android.com/apk/res/android">

     <group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/nav_home" />
    <item
        android:id="@+id/nav_photos"
        android:icon="@drawable/ic_photo_library_black_24dp"
        android:title="@string/nav_photos" />
    <item
        android:id="@+id/nav_movies"
        android:icon="@drawable/ic_local_movies_black_24dp"
        android:title="@string/nav_movies" />

    <item
        android:id="@+id/nav_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/nav_notifications" />

    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_settings_black_24dp"
        android:title="@string/nav_settings" />
</group>

<item android:title="Other">
    <menu>
        <item
            android:id="@+id/nav_about_us"
            android:title="@string/nav_about_us" />
        <item
            android:id="@+id/nav_privacy_policy"
            android:title="@string/privacy_policy" />
    </menu>
</item>

像这样:

enter image description here

java类使用菜单栏

      private void selectNavMenu() {
    navigationView.getMenu().getItem(navItemIndex).setChecked(true);
     }

    private void setUpNavigationView() {
    //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {
                //Replacing the main content with ContentFragment Which is our Inbox View;
                case R.id.nav_home:
                    navItemIndex = 0;
                    CURRENT_TAG = TAG_HOME;
                    break;
                case R.id.nav_photos:
                    navItemIndex = 1;
                    CURRENT_TAG = TAG_PHOTOS;
                    break;
                case R.id.nav_movies:
                    navItemIndex = 2;
                    CURRENT_TAG = TAG_MOVIES;
                    break;
                case R.id.nav_notifications:
                    navItemIndex = 3;
                    CURRENT_TAG = TAG_NOTIFICATIONS;
                    break;
                case R.id.nav_settings:
                    navItemIndex = 4;
                    CURRENT_TAG = TAG_SETTINGS;
                    break;
                case R.id.nav_about_us:
                    // launch new intent instead of loading fragment
                    startActivity(new Intent(MainActivity.this, AboutUsActivity.class));
                    drawer.closeDrawers();
                    return true;
                case R.id.nav_privacy_policy:
                    // launch new intent instead of loading fragment
                    startActivity(new Intent(MainActivity.this, PrivacyPolicyActivity.class));
                    drawer.closeDrawers();
                    return true;
                default:
                    navItemIndex = 0;
            }

            //Checking if the item is in checked state or not, if not make it in checked state
            if (menuItem.isChecked()) {
                menuItem.setChecked(false);
            } else {
                menuItem.setChecked(true);
            }
            menuItem.setChecked(true);

            loadHomeFragment();

            return true;
        }
    });

它可以帮助你

答案 1 :(得分:0)

试试这个

使用visible true / false而不是添加/删除

 navigationView = (NavigationView) findViewById(R.id.nav_view);
 Menu nav_Menu = navigationView.getMenu();
 nav_Menu.findItem(R.id.nav_settings).setVisible(false);