Создать глобальную переменную.Измените его состояние, когда ваше условие выполнено.Не добавляйте пункты меню, которые вы хотите скрыть, в 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);
}
}