java - 导航时关闭上下文操作栏。抽屉已切换

标签 java android navigation android-actionbar

official page for the Navigation Drawer design pattern状态:

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.

但是经过研究,我似乎找不到一种方法来“关闭”我的

中的上下文操作栏
@Override
public void onDrawerOpened(View drawerView) {
    // ... My Code ...
}

方法。

在我的例子中,当用户从本身显示导航的 Activity 中的 EditText 选择文本时,可能会出现 CAB(带有复制、粘贴等选项) 。抽屉。

我见过this question+answer但它并不能完全解决我的问题,因为它与自定义 ActionMode 有关。每当切换抽屉导航时,如何“关闭”CAB(用户选择文本时显示的 CAB)?

最佳答案

这是可能的。您必须在创建 ActionMode 时获取对它的引用,并在 Activity 中获取对 ActionMode.Callback 的引用:

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

然后,当您的抽屉打开/关闭时,完成 ActionMode 或从 ActionMode.Callback 启动新的 ActionMode:

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

@Override
public void onDrawerClosed(View drawerView) {
    if (mActionModeCallback != null) {
        startActionMode(mActionModeCallback);
    }
}

关于java - 导航时关闭上下文操作栏。抽屉已切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23475559/

相关文章:

android - 如何设计 React-Native Android 应用程序的导航器结构?

java - 在Java中转换日期格式的问题

java - ImageIO.read 正在终止程序,没有错误

java - 在android中使用kso​​ap2上传的空对象

Android ViewPager2如何检索 fragment onBindViewHolder

android - 应用内计费或许可?

java - 从 JSF 操作返回 null 和 ""之间的区别

ruby-on-rails - Bootstrap动态主动导航

java - 更改 fragment 内的布局参数

android - 在 Android WebView 中从 CSS 文件加载背景图像