Hiding contexual action bar while navigation drawer is open
Asked Answered
A

3

11

The question is similar to this except for the fact that instead of using a View Pager (VP) I want to use a Navigation Drawer (ND). I have a list of elements that activates a Contextual Action Bar (CAB) when one/several items are selected through long press actions. When the ND is opened I want to hide temporarily the CAB and show the basic action bar, while when it is closed to show again the CAB with the selected items.

The feature is specified in the ND official documentation "Hide contextual action bars while the drawer is visible.". So far I couldn't find anywhere how to make it in a nice way, but I tried a workaround:

  • Finish CAB when ND opens and save the selected items (if any).
  • When drawer closes, start the action mode that enables the CAB and populate the list of selected items from the list saved.

An example of what I am trying to achieve can be experienced with the Gmail application.

Thank you in advance for any answer!

Antagonism answered 22/1, 2014 at 16:18 Comment(0)
I
4

To achieve this requirement, I am calling these two methods from the ActionBarDrawerToggle callback methods:

public void onDrawerOpened() {
  if (mActionMode != null) {
    mCheckedListItems = mListView.getCheckedItemPositions().clone();
    mActionMode.finish();
  }
}
public void onDrawerClosed() {
  if (mCheckedListItems!=null) {
    for (int i=0; i<mCheckedListItems.size(); i++) {
      if (mCheckedListItems.valueAt(i)) {
        mListView.setItemChecked(mCheckedListItems.keyAt(i), true);
      }
    }
  }
  mCheckedListItems = null;
}
Ishtar answered 27/1, 2014 at 11:52 Comment(1)
My approach was similar, but you are also keeping a list of checked items. I thought there can be a more natural way without this workaround. Anyway, thanks. Though I will wait for other opinions before accepting your response.Antagonism
P
4

If you're using a material design styled navigation drawer, the accepted solution doesn't look all that nice as the ActionMode sits on top of the drawer until it's fully open.

An alternative is to use onDrawerStateChanged instead, then as soon as you start dragging the drawer it will hide the ActionMode:

@Override
public void onDrawerStateChanged(int newState) {
    super.onDrawerStateChanged(newState);
    mActionMode.finish();
}
Pulvinus answered 24/2, 2015 at 1:57 Comment(0)
G
1

The Sprockets library (disclosure: I'm the developer) does this automatically when extending NavigationDrawerActivity and SprocketsListFragment. Instead of the latter, it's also possible to extend SprocketsFragment and provide your AbsListView in getAbsListView(). When the navigation drawer is opened, the ActionMode will be hidden. And when it is closed, the ActionMode will be restored.

Galosh answered 9/12, 2014 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.