【问题标题】:Creating Separate OnClick Listeners for Left and Right Navigation Drawers为左右导航抽屉创建单独的 OnClick 侦听器
【发布时间】:2015-07-13 19:38:30
【问题描述】:

我目前有两个导航抽屉/菜单:

但是,我需要一些帮助来为正确的操作栏菜单设置 OnClick 侦听器。

目前 - 我有一个左侧操作栏菜单的 onclick 侦听器

   @Override
        public boolean onOptionsItemSelected(MenuItem item) {


            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }


            return super.onOptionsItemSelected(item);
        }

但是我不确定如何为正确的操作栏菜单处理 onClick 事件。

非常感谢任何建议。

完整的 XML 和 Java 源代码:

http://pastebin.com/ygyyjtLZ

【问题讨论】:

    标签: android android-xml menuitem android-menu


    【解决方案1】:

    试试这个:

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
    
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            } else {
                switch (item.getItemId()) {
                   case R.id.action_more: // change the action_more with your own id in main.xml menu
                     // do your stuff here (your right click method)
                     return true;
                   // rest of your menu actions here...
               }
            }
    
            return super.onOptionsItemSelected(item);
     }
    

    【讨论】:

    • 我尝试了您建议的代码 - 但是在 if (mDrawerToggle.onOptionsItemSelected(item)) { 设置断点并单击右侧菜单按钮时 - 调试时未达到:/(这很奇怪- 考虑到它在案例陈述之前)
    • 你使用的是 Fragment 还是 Activity?
    • Activity - 完整的 Java 和 XML 源代码可以在这里找到:pastebin.com/ygyyjtLZ(感谢您的帮助 - 这让我发疯了)
    • 奇怪的是,我似乎无法在右侧菜单按钮上注册/监听 onClick 事件......我真的不明白这是为什么。
    • 尝试返回 super.onCreateOptionsMenu() 而不是 true (在 onCreateOptionsMenu() 方法中)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    相关资源
    最近更新 更多