Dismiss the Contextual Action Bar When Nav. Drawer

2019-07-19 17:56发布

The official page for the Navigation Drawer design pattern states:

Sometimes the user will be in a state where a contextual action bar (CAB) appears instead of the app’s action bar. This typically happens when the user selects text or selects multiple items after a press-and-hold gesture. While the CAB is visible, you should still allow the user to open the navigation drawer using an edge swipe. However, replace the CAB with the standard action bar while the navigation drawer is open. When the user dismisses the drawer, re-display the CAB.

But after researching I can't seem to find a way to "dismiss" the Contextual Action Bar inside my

@Override
public void onDrawerOpened(View drawerView) {
    // ... My Code ...
}

method.

In my case a CAB (with copy, paste, etc. options) may appear when a user selects text from an EditText in an Activity which itself displays a Nav. Drawer.

I've seen this question+answer but it doesn't quite fix my problem as it's related to a custom ActionMode. How can I "dismiss" the CAB - the one that shows up when a user selects text - whenever the navigation drawer is toggled?

1条回答
甜甜的少女心
2楼-- · 2019-07-19 18:11

It is possible. You have to grab a reference to the ActionMode when it is created, and the ActionMode.Callback in your Activity:

@Override
public void onActionModeStarted(ActionMode mode) {
    super.onActionModeStarted(mode);
    mActionMode = mode;
}

@Override
public void onActionModeFinished(ActionMode mode) {
    super.onActionModeFinished(mode);
    mActionMode = null;
}

@Override
public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
    mActionModeCallback = callback;
    return super.onWindowStartingActionMode(callback);
}

Then when your drawer opens/closes, finish the ActionMode or start a new ActionMode from the ActionMode.Callback:

@Override
public void onDrawerOpened(View drawerView) {
    if (mActionMode != null) {
        mActionMode.finish();
    }
}

@Override
public void onDrawerClosed(View drawerView) {
    if (mActionModeCallback != null) {
        startActionMode(mActionModeCallback);
    }
}
查看更多
登录 后发表回答