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

后端 未结 5 1223
攒了一身酷
攒了一身酷 2020-12-19 02:44

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

相关标签:
5条回答
  • 2020-12-19 03:15

    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;
        }
    });
    
    0 讨论(0)
  • 2020-12-19 03:21

    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;
        }
    });
    
    0 讨论(0)
  • 2020-12-19 03:25

    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
    }
    
    0 讨论(0)
  • 2020-12-19 03:29

    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;
    }
    
    0 讨论(0)
  • 2020-12-19 03:37

    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;
        }
    });
    
    0 讨论(0)
提交回复
热议问题