如何使用新的NavigationView获取MenuItem在侦听器中的位置 [英] How to get MenuItem position in the listener using the new NavigationView

查看:174
本文介绍了如何使用新的NavigationView获取MenuItem在侦听器中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个话题说明了一切.我应该如何使用NavigationView检索onClick侦听器上的项目位置?另外,为什么没有getHeader方法?最后,我正在以编程方式进行所有操作,但是标题仍然可以单击.有什么想法吗?

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 doing everything programmatically, but the header is still clickable. Any thoughts?

谢谢!

推荐答案

更新

您可以使用此技巧获得位置

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

这篇关于如何使用新的NavigationView获取MenuItem在侦听器中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆