Kris B
Kris B

Reputation: 3578

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?

Upvotes: 0

Views: 889

Answers (2)

Gv Ravi
Gv Ravi

Reputation: 391

override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenu.ContextMenuInfo?) {
    if(showContextMenu) {
        // inflate the menu layout
    }
    else {
        return; // this will show noting, but click action will continue
    }
}

Upvotes: 0

David Guo
David Guo

Reputation: 1759

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.

Upvotes: 1

Related Questions