Android: problem with a menu item that has been repeatedly checked

This is the beahviour of my application (of course, only 1):

enter image description here

Of course, I want only one item to be checked at a time.

I separated the elements in two groups (to add a separator, see my previous question: How to add a horizontal separator in navdrawer? )

This is the nav_menu.xml file:

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

    <group android:checkableBehavior="single"
        android:id="@+id/group1" >

        <item
            android:id="@+id/home"
            android:checked="false"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="@string/list_home" />

        <item
            android:id="@+id/list_event"
            android:checked="false"
            android:icon="@drawable/ic_list_black_24dp"
            android:title="@string/list_event" />

    </group>

    <group
        android:checkableBehavior="single"
        android:id="@+id/group2" >

        <item
            android:id="@+id/settings"
            android:checked="false"
            android:icon="@drawable/ic_settings_black_24dp"
            android:title="@string/settings" />

    </group>


</menu>

This is BaseApp that controls NavDrawer:

package com.xx.views;

import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBarDrawerToggle;
import android.os.Bundle;
import android.view.View;

import com.xx.R;
import com.xx.mappers.DateManager;

public class BaseApp extends AppCompatActivity {

    //Defining Variables
    protected String LOGTAG = "LOGDEBUG";
    protected Toolbar toolbar;
    protected NavigationView navigationView;
    protected DrawerLayout drawerLayout;

    private DateManager db = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_layout);

        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame, new DashboardFragment());
        fragmentTransaction.commit();

        setNavDrawer();

        // make home as checked
        navigationView.getMenu().getItem(0).setChecked(true);
    }

    private void setNavDrawer(){

        // Initializing Toolbar and setting it as the actionbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Initializing NavigationView

        //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) {


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

                //Closing drawer on item click
                drawerLayout.closeDrawers();


                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {

                    case R.id.home:

                        DashboardFragment dashboardFragment = new DashboardFragment();
                        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, dashboardFragment,"DASHBOARD_FRAGMENT");
                        fragmentTransaction.commit();
                        return true;

                    case R.id.list_event:
                        ListEventFragment fragmentListEvent = new ListEventFragment();
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, fragmentListEvent);
                        fragmentTransaction.commit();
                        return true;

                    case R.id.settings:
                        SettingsFragment fragmentSettings = new SettingsFragment();
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, fragmentSettings);
                        fragmentTransaction.commit();
                        return true;

                    default:

                        return true;

                }
            }
        });

        // Initializing Drawer Layout and ActionBarToggle
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        ActionBarDrawerToggle actionBarDrawerToggle =
                new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open_drawer, R.string.close_drawer){

                    @Override
                    public void onDrawerClosed(View drawerView) {
                        // Code here will be triggered once the drawer closes as we dont want anything
                        // to happen so we leave this blank
                        super.onDrawerClosed(drawerView);
                    }

                    @Override
                    public void onDrawerOpened(View drawerView) {
                        // Code here will be triggered once the drawer open as we dont want anything
                        // to happen so we leave this blank
                        super.onDrawerOpened(drawerView);
                    }
                };

        //Setting the actionbarToggle to drawer layout
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        //calling sync state is necessay or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();
    }

    private void eraseTable(){
        db=new DateManager(this);
        db.resetTable();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

Many thanks

+4
source share
4 answers

: android: checkableBehavior = "single" xml android: checkable = "true" , MenuItem onNavigationItemSelected, MenuItem , false , menuItem, MenuItem.

.

if (prevMenuItem != null) {
   prevMenuItem.setChecked(false);
}

menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
prevMenuItem = menuItem;
return true;

+6

groups android:checkableBehavior="single", , , ( , item.setChecked(true)):

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    int id = item.getItemId();

    //item.setChecked(true); //Won't work, will leave previous item checked too.
    navigationView.setCheckedItem(id); //this will check single item

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

, , .

+1

, /, , .

I suggest you use this library . Its implementation of the material navigation box, it has already implemented all this logic. If you do not want to go with lib, you should check them out how to do this on this library code and adapt to your needs.

0
source

Put both groups within the same group in a set

android:checkableBehavior="single"

Create a parent group

0
source

All Articles