无法在操作栏项目上单击打开导航抽屉

时间:2015-03-30 16:01:41

标签: android menu navigation-drawer

我希望在菜单点击时打开正确的抽屉 我使用了以下代码,但由于某种原因它无法正常工作

mDrawerToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,toolbar, R.string.drawer_open,
            R.string.drawer_close) {

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item != null && item.getItemId() == R.id.more) {
                if (mDrawerLayout.isDrawerOpen(Gravity.END)) {
                    saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,mUserLearnedDrawer+"");
                    mDrawerLayout.closeDrawer(Gravity.END);
                } else {
                    mDrawerLayout.openDrawer(Gravity.END);
                }
            }
            return false;
        }
    };

请帮帮我

  

通过修改主要活动来解决这个问题

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        case R.id.more:
            if (notificationDrawerFragment.mDrawerLayout.isDrawerOpen(Gravity.END)){
                notificationDrawerFragment.mDrawerLayout.closeDrawer(Gravity.END);
            }
            else
            { notificationDrawerFragment.mDrawerLayout.openDrawer(Gravity.END);
            }

        default:
            return super.onOptionsItemSelected(item);
    }
}

3 个答案:

答案 0 :(得分:1)

通过修改主要活动

中的菜单选项解决了以下问题
 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        case R.id.more:
            notificationDrawerFragment.mDrawerLayout.openDrawer(Gravity.END);

        default:
            return super.onOptionsItemSelected(item);
    }
}

答案 1 :(得分:0)

你有这个覆盖:

@Override
protected void onPostCreate(Bundle savedInstanceState) {
      super.onPostCreate(savedInstanceState);
      mDrawerToggle.syncState();
}

你也应该在ActionBarDrawerToggle

下进行覆盖
public void onDrawerOpened(View drawerView) and
public void onDrawerClosed(View drawerView)

答案 2 :(得分:0)

试试这个,我从SO中学到了这个:

@Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item != null && item.getItemId() == android.R.id.more) {
                if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,mUserLearnedDrawer+" ");
                    mDrawerLayout.closeDrawer(Gravity.RIGHT);
                } else {
                    mDrawerLayout.openDrawer(Gravity.RIGHT);
                }
            }
            return false;
        }
    };

希望这有帮助。