开发者

Hide Android Context Menu Item

Another newbie question. I have a context menu that I apply for a ListView that simply allows the user to move items up or down, or delete the item.

I have code in onContextItemSelected() to prevent things from moving up past top or bottom of the list, etc.,开发者_运维问答 but I'd rather hide the context menu items in the first place if (for instance) the top item in the list is selected.

I assume that I need to do this in onCreateContextMenu, but I'm not sure how.

Here is my onCreateContextMenu code:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mycontextmenu, menu);
}

Thanks,

wTs


In your onCreateContextMenu method, you need to get the menu items that you potentially want to hide and set them as not visible based on the list positions.

Something like this:

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

if(info.position < 1) {
   myLocationMenuItem = menu.findItem(R.id.myLocation);
   myLocationMenuItem.setVisible(enable);
}


This is a solution for a PopupMenu just in case somebody is seeking for it as I did. Here I have 3 buttons in menu layout file for PopupMenu and I remove some of them in some cases:

    PopupMenu popup = new PopupMenu(this, this.actionButton);
    popup.setOnMenuItemClickListener(this);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_resume_view, popup.getMenu());

    Menu menu = popup.getMenu();
    if (this.resume.isPublished()) {
        menu.removeItem(R.id.menu_button_publish);
    }
    else {
        menu.removeItem(R.id.menu_button_unpublish);
        menu.removeItem(R.id.menu_button_update_publish_date);
    }

    popup.show();

When you use PopupMenu onPrepareOptionsPanel is not called. So you have to get menu items by id while creating PopupMenu and remove those, that should not be available in a particular case.


If a context menu is opened for a ListView, menuInfo will contain an object of type AdapterContextMenuInfo, which gives you information about which item in the list was clicked. If it is the first or the last item, you can simply remove the corresponding entries from the context menu, though I'm not quite sure what happens if no entries are left.


You can Disable a particular item if you want to.

@Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo)
     {
                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

                int position = info.position;

                // use this position to decide your item clicked

                menu.clear();
                menu.setHeaderTitle("Context Menu Title");
                String[] menuItems = getResources().getStringArray(
                        R.array.menu_context);


                for (int i = 0; i < menuItems.length; i++)
                {
                    menu.add(Menu.NONE, i , i, menuItems[i]);
                }
               menu.getItem(0).setEnabled(**Conditional check**);
               menu.getItem(1).setEnabled(**Conditional check**);
               menu.getItem(2).setEnabled(**Conditional check**);
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜