The topic says it all. How should I go about retrieving the item position on the onClick listener using NavigationView? Also, why is there no getHeader method? Lastly I am d
I found a simple solution. You can assign an order using Menu's add(...) method. Then you can retrieve the order using MenuItems's getOrder(...) method. If you are using xml, you can use android:orderInCategory="..."
.
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);.
Menu menu = navigationView.getMenu();
for(int i=0; i < menu.size(); i++){
items.add(Menu.NONE, Menu.NONE, i, menu.getItem(i));
}
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
// update highlighted item in the navigation menu
menuItem.setChecked(true);
int position=items.getOrder();
return true;
}
});
UPDATE
You can get position using this trick
final List<MenuItem> items = new ArrayList<>();
Menu menu;
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);.
menu = navigationView.getMenu();
for(int i = 0; i < menu.size(); i++){
items.add(menu.getItem(i));
}
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
// update highlighted item in the navigation menu
menuItem.setChecked(true);
int position = items.indexOf(menuItem);
return true;
}
});
You can just take its order if you specify the "android:orderInCategory" attribute for menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:orderInCategory="0"
android:title="@string/news" />
<item
android:orderInCategory="1"
android:title="@string/search" />
</menu>
val navigationView = findViewById<NavigationView>(R.id.navigation)
navigationView.setNavigationItemSelectedListener { menuItem ->
val menuItemOrder = menuItem.order
true
}
Or, use this in case you don't want to specify orders by hand:
val navigationView = findViewById<NavigationView>(R.id.navigation)
navigationView.setNavigationItemSelectedListener { menuItem ->
val menuItemIndex = bottomNavigation.menu.children.indexOf(menuItem)
true
}
In my case, i use
first test whit this..
Log.d(TAG, navigationView.getMenu().getItem(0).isChecked());
Log.d(TAG, navigationView.getMenu().getItem(1).isChecked());
Log.d(TAG, navigationView.getMenu().getItem(2).isChecked());
next this...
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(navigationView.getMenu().getItem(0).isChecked()) {
cerrarSesion();
}else {
navigationView.getMenu().getItem(0).setChecked(true);
seleccionarItem(navigationView.getMenu().getItem(0));
}
}
return false;
}
If you are using menu_drawer.xml
, you just have to add an id
in the items like this:
<item
android:id="@+id/nav_top_stories"
android:title="@string/txt.menu.item1"
/>
With this you just have to test on menuItm.getId()
:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
// update highlighted item in the navigation menu
menuItem.setChecked(true);
switch(menuItem.getId()){
case R.id.txt_menu_item1 : //do what you want to do;
break;
case R.id.txt_menu_item2 : // etc,
}
return true;
}
});
If you are using dynamic menu, just use this method to add an item to you navigation drawer:
NavigationView.getMenu().add(int groupId, int itemId, int order, CharSequence title)
And then test by the order:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
// update highlighted item in the navigation menu
menuItem.setChecked(true);
switch(menuItem.getOrder()){
case 0 : //do what you want to do;
break;
case 1 : // etc,
default : //do whatever you want ;
}
return true;
}
});