android 上下文操作模式有时不显示

waxmsbnn  于 2022-11-27  发布在  Android
关注(0)|答案(1)|浏览(150)

自从升级到compileSDK 31(Android 12)以来,一些用户遇到了一个非常奇怪的问题。
在Recycler视图中选择一个列表项后,操作菜单与我的编辑按钮不显示。列表项显示为已选择,但操作菜单不可见(见屏幕截图):

无选择:

已选择:

如果正在工作:

如果用户关闭或杀死应用程序并重新打开它,它会再次工作。一段时间后,问题再次出现。
奇怪的是代码被执行了。例如,你看到状态栏被设置为灰色。
我的日志语句来自方法:

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

使用“布局检查器”更新某些分析:

如果不工作,则宽度和高度为0dp:

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


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

hlswsv35

hlswsv351#

我建议你有一个局部变量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;
}

因为在这部分之后,您将检查:“如果已显示动作模式CAB”,并跳过新模式的显示。
让我知道-帮助与否:)

相关问题