隐藏上下文菜单项

时间:2018-05-31 07:26:41

标签: android android-recyclerview recycler-adapter android-contextmenu

我的应用程序中有一个上下文菜单,但我想在指定特定条件时隐藏其项目。 我该怎么办?

这是onCreateContextMenu代码

   @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        menu.setHeaderTitle("Select the Action");

        menu.add(0,0,getAdapterPosition(), Common.EDIT_POST);
        menu.add(0,1,getAdapterPosition(),Common.DELETE_POST);

    }

1 个答案:

答案 0 :(得分:0)

创建一个全局变量。满足条件时改变其状态。不要将要隐藏的菜单项添加到menu

// Declare as global inside Activity
private customCondition = true;

...
// Check if your condition has been met and change the variable state
if(isConditionMet()) {
   customCondition = false;
} else {
   customCondition = true;
}
...

现在在onCreateContextMenu内,

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

    menu.setHeaderTitle("Select the Action");
    menu.add(0, 0, getAdapterPosition(), Common.EDIT_POST);

    if(!customCondition) {
        // Hide the delete post menuitem
        menu.add(0, 1, getAdapterPosition(),Common.DELETE_POST);
    }

}