Search code examples
androidcontextmenuandroid-listview

Disable context menu on certain ListView items


I have a ListView with a ContextMenu associated with each item in the ListView.

Is it possible to disable the ContextMenu for specific items?


Solution

  • I had the same issue before, following is my solution: I set my ListView's OnItemLongClickListener as following:

    private OnItemLongClickListener mBookLongClickListener = new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View view, int id,
                long arg3) {
            showBookDialog(view, id);
            return true;
        }
    };
    

    In the method showBookDialog(): According to the user's current item, I create an AlertDialog with option list(something like menu) or show nothing at all.