【问题标题】:Contextual Action Mode sometimes not shown有时不显示上下文操作模式
【发布时间】:2022-12-09 01:23:15
【问题描述】:

自从升级到编译SDK 31(Android 12),一些用户遇到了一个非常奇怪的问题。

在 Recyclerview 中选择一个列表项后操作菜单未显示用我的编辑按钮。列表项显示为选中状态,但操作菜单不可见(参见屏幕截图):

没有选择:

已选:

如果它正在工作:

如果用户关闭或终止应用程序并重新打开它,它会再次运行。 一段时间后,问题再次出现。

奇怪的是代码被执行了。例如,您看到状态栏设置为灰色。

我的方法日志语句:

2022-11-25 13:06:14.312 20525-20525 ActiveFragment com.xxx.xxxx I onItemClick - Position 1
2022-11-25 13:06:14.349 20525-20525 ActiveFragment com.xxx.xxxx I onCreateActionMode is called.
2022-11-25 13:06:14.350 20525-20525 ActiveFragment com.xxx.xxxx I onCreateActionMode - set status bar color.
2022-11-25 13:06:14.375 20525-20525 ActiveFragment com.xxx.xxxx I onCreateActionMode - Inflate menu_options and returning true.
2022-11-25 13:06:14.376 20525-20525 ActiveFragment com.xxx.xxxx I onPrepareActionMode is called.
2022-11-25 13:06:14.386 20525-20525 ActiveFragment com.xxx.xxxx I onPrepareActionMode - returning true.
2022-11-25 13:06:14.542 20525-20525 ActiveFragment com.xxx.xxxx I onPrepareActionMode is called.
2022-11-25 13:06:14.553 20525-20525 ActiveFragment com.xxx.xxxx I onPrepareActionMode - returning true.
2022-11-25 13:06:14.554 20525-20525 ActiveFragment com.xxx.xxxx I onItemClick - Starting the action mode and setting the title Options.

我的代码:

@Override
public boolean onItemClick(View view, int position) {
    HyperLog.i(TAG, "onItemClick - Position " + position);
    
    if(position == RecyclerView.NO_POSITION) {
        HyperLog.e(TAG, "onItemClick - Position was NO_POSITION. Returning false.");
        return false;
    }

    flexibleAdapter.toggleSelection(position);

    // If no item is selected close the Action Mode CAB
    if (checkedCount == 0) {
        if(mActionMode != null) {
            mActionMode.finish();
        }
        HyperLog.e(TAG, "onItemClick - Checked Item Count is 0, not showing ActionMode.");
        return true;
    }

    // If the Action Mode CAB is already displayed return
    if (mActionMode != null) {
        HyperLog.e(TAG, "onItemClick - Action Mode is already displayed. Return true.");
        return true;
    }

    // Start the CAB using the ActionMode.Callback defined above
    mActionMode = activity.startSupportActionMode(mActionModeCallback);
    if(mActionMode != null) {
        mActionMode.setTitle(R.string.options);
        mActionMode.invalidate();
        HyperLog.i(TAG, "onItemClick - Starting the action mode and setting the title Options.");
    }

    return true;
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    HyperLog.i(TAG, "onCreateActionMode is called.");
    
    //hold current color of status bar
    statusBarColor = activity.getWindow().getStatusBarColor();

    //set your gray color
    activity.getWindow().setStatusBarColor(tools.getColor(R.color.cab_color_dark));
    HyperLog.i(TAG, "onCreateActionMode - set status bar color.");

    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(R.menu.menu_options, menu);
    HyperLog.i(TAG, "onCreateActionMode - Inflate menu_options and returning true.");

    return true;
}

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {

        //return to "old" color of status bar
        activity.getWindow().setStatusBarColor(statusBarColor);

        mActionMode = null;
        selectionType = -1;
        flexibleAdapter.clearSelection();
    }

使用“LayoutInspector”更新一些分析:

如果它不起作用,宽度和高度为 0dp:

如果它正在工作,则设置宽度和高度:

那么为什么不将框架设置为上下文操作栏的宽度和高度呢?

编辑 08.12.2022:
发生这种情况时,我现在再次进行了一些调试。
每次我在应用程序中选择或取消选择一个项目时,日志猫都会向我显示这一行(进程是 system.err):

【问题讨论】:

    标签: android android-recyclerview action-menu


    【解决方案1】:

    正如我建议您有一个局部变量 mActionMode,我想您可能忘记将其重置为 null 值?

    // If no item is selected close the Action Mode CAB
    if (checkedCount == 0) {
        if(mActionMode != null) {
            mActionMode.finish();
            mActionMode = null; <- TRY ADD THIS LINE
        }
        HyperLog.e(TAG, "onItemClick - Checked Item Count is 0, not showing ActionMode.");
        return true;
    }
    

    因为在该部分之后您将检查:“如果 Action Mode CAB 已显示”并跳过新的显示。

    让我知道 - 帮助与否:)

    【讨论】:

    • 谢谢,但我在“onDestroyActionMode”中有 mActionMode = null。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多