弹出菜单正在累积事件(未正确消除)

时间:2018-09-17 15:16:25

标签: android popupmenu

我的应用程序中有一个奇怪的错误。我有一个RecyclerView,其中包含带有溢出菜单图标的列表项。参见下文。enter image description here

当我触摸菜单外部(以取消菜单)时,会出现问题。下次选择菜单时,它将呈现2个菜单。这与我进行议案时一样多。见下文。 enter image description here

我不知道为什么会这样。这是我的代码,用于在ViewHolder的列表项中显示菜单:

final PopupMenu popupMenu = new PopupMenu(mContext, mOptionsMenu);

                mOptionsMenu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        popupMenu.inflate(R.menu.menu_edit_delete_dq_competitor);

                        Menu menu = popupMenu.getMenu();
                        MenuItem edit = menu.findItem(R.id.edit);
                        MenuItem delete = menu.findItem(R.id.delete);
                        MenuItem dq = menu.findItem(R.id.dq);
                        MenuItem undoDq = menu.findItem(R.id.undo);

                        // change options if this competitor is DQ
                        if (competitor.getStatus() == Status.DQ) {
                            edit.setVisible(false);
                            delete.setVisible(false);
                            dq.setVisible(false);
                            undoDq.setVisible(true);
                        }
                        popupMenu.show();
                    }
                });

这是我选择菜单项时的代码:

      popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        FragmentManager manager = getActivity().getSupportFragmentManager();
                        switch (item.getItemId()) {
                            case R.id.edit:
                                // todo finish this
//                                Intent intent = CompetitorInMatchForm.editIntent(mContext, competitor, true);
//                                startActivity(intent);
                                break;
                            case R.id.delete:
                                CompetitorStatusInMatchDialog deleteDialog = CompetitorStatusInMatchDialog.newInstance(competitor, "delete", getAdapterPosition());
                                deleteDialog.setTargetFragment(CompsInMatchFragment.this, REQUEST_ACTION);
                                deleteDialog.show(manager, DELETE_COMP_FROM_MATCH);
                                break;
                            case R.id.dq:
                                CompetitorStatusInMatchDialog dqDialog = CompetitorStatusInMatchDialog.newInstance(competitor, "dq", getAdapterPosition());
                                dqDialog.setTargetFragment(CompsInMatchFragment.this, REQUEST_ACTION);
                                dqDialog.show(manager, DELETE_COMP_FROM_MATCH);
                                break;
                            case R.id.undo:
                                CompetitorStatusInMatchDialog undoDialog = CompetitorStatusInMatchDialog.newInstance(competitor, "undo", getAdapterPosition());
                                undoDialog.setTargetFragment(CompsInMatchFragment.this, REQUEST_ACTION);
                                undoDialog.show(manager, DELETE_COMP_FROM_MATCH);
                                break;
                            default:
                                popupMenu.dismiss();
                        }

                        return true;
                    }
                });

任何有助于确定对话框为什么会累积菜单出现的帮助都会很大。我以前使用过弹出菜单,但没有出现此问题。我希望您在菜单外触摸时,它会被关闭。

1 个答案:

答案 0 :(得分:0)

那是因为您在onClick侦听器中增加了菜单,因此实际上每次显示菜单时都会添加所有元素。

如果您期望dismiss从菜单中删除项目,那是错误的:它只会隐藏弹出窗口。

您可以将此行移到侦听器之外:

popupMenu.inflate(R.menu.menu_edit_delete_dq_competitor);

像这样:

final PopupMenu popupMenu = new PopupMenu(mContext, mOptionsMenu);
popupMenu.inflate(R.menu.menu_edit_delete_dq_competitor);