Hiding contexual action bar while navigation drawer is open

强颜欢笑 提交于 2019-11-30 18:43:14

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!