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

后端 未结 5 1248
攒了一身酷
攒了一身酷 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:25

    You can just take its order if you specify the "android:orderInCategory" attribute for menu items:

    
    
        
        
    
    
    val navigationView = findViewById(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(R.id.navigation)
    
    navigationView.setNavigationItemSelectedListener { menuItem ->
        val menuItemIndex = bottomNavigation.menu.children.indexOf(menuItem)
    
        true
    }
    

提交回复
热议问题