Reputation: 16793
I have implemented navigation drawer, it is working fine. The only problem that I have to make selected item to deselect when the navigationdrawer closes. I wonder how could I able to get the index value of selected menu item.
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(false);
}
)}
Upvotes: 15
Views: 21308
Reputation: 11
This is all you need, it deselects and selects the new one
navigationView.setCheckedItem(item.getItemId());
And it should go at the bottom of onNavigationItemSelected.
To make it more clear here's an example
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
controller.setFragment(new HomeFragment());
break;
case R.id.nav_settings:
controller.setFragment(new SettingsFragment());
break;
case R.id.nav_logout:
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.putExtra(autoLogin, false);
startActivity(intent);
break;
}
navigationView.setCheckedItem(item.getItemId());
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return false;
}
Upvotes: 1
Reputation: 37
Another easy way that worked for me
public boolean onNavigationItemSelected(MenuItem item) {
int index = (item.getItemId() % item.getGroupId())-1;
....
}
I think, this would be much more efficient than using For loop or Switch case.
If you still have problems then my suggestion would be to check your Item Id and group Id in Log-cat so that you would have a better understanding of what you are dealing with.
Upvotes: 1
Reputation: 17729
First assign your navigationMenu to a class field
private NavigationView navigationView;
private int currentPosition;
...
navigationView = (NavigationView) findViewById(R.id.nav_view);
Now in the menu callback you can loop over the menu items :
public boolean onNavigationItemSelected(MenuItem item) {
for (int i=0;i<navigationView.getMenu().size();i++){
if(item==navigationView.getMenu().getItem(i)){
currentPosition=i;
break;
}
}
....
}
You can change the order of the menu items without breaking anything
Upvotes: 1
Reputation: 1077
Try this to get navigation item at position 0 getItem(index) will give you desired item
navigationView.getMenu().getItem(selectedposition).setChecked(false);
also use this link for reference for getting seleted item http://thegeekyland.blogspot.in/2015/11/navigation-drawer-how-set-selected-item.html
use below code for getting selected position
int selectedposition= 0;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.firstitemid:
selectedposition= 0;
break;
case R.id.seconditemid:
selectedposition= 1;
break;
case R.id.thirditemid:
selectedposition= 2;
break;
}
return true;
}
});
Upvotes: 3
Reputation: 27515
One way to do is store menu item ID in variable e.g checkedItemID
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItemID=menuItem.getItemId();
}
)}
Then on implement
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
// Do whatever you want here
navigationView.getMenu().findItem(menuItemID).setChecked(false);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// Do whatever you want here
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
Upvotes: 16
Reputation: 1386
Make a variable position of int
datatype. Firstly set its value to 0 and onNavigationItemSelected change its value to menuItem
index(like 0 or 1 or 2 and so on).Now this position will provide you the index of selected menuItem
.
int position = 0;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.first:
position = 0;
break;
case R.id.second:
position = 1;
break;
case R.id.third:
position = 2;
break;
}
return true;
}
});
Upvotes: 5