How to get MenuItem position in the listener using the new NavigationView

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

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

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
}

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