Anonymous
Anonymous

Reputation: 4910

android - block context menu from appearing

I have a sortable ListView with TouchInterceptor by GoogleMusic to reorder the list items at drag. I also have a ContextMenu registered for this ListView. The problem is that dragging an item to change it's position is considered a long press by the system and context menu appears when it shouldn't. My first thought was to have a boolean variable set to true if onDrag() is called and false when drag stops, and set the onCreateContextMenu like this:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        if (v.getId()==R.id.listView1 && !drag) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
            String title = data[info.position];
            menu.setHeaderTitle(title);
            String[] menuItems = getResources().getStringArray(R.array.actions_menu); 
            for (int i = 0; i<menuItems.length; i++) {
                menu.add(Menu.NONE, i, i, menuItems[i]);
            }
        }
    }

That didn't work so well so I set the drag=false a second after the drag was over using a handler.postDelayed(). This works ok for the most part, but leaves the position of the ListView from which the drag started, looking like it is pressed.

I also tried unregistering and re-registering for context menu at drag events..also didn't work. the menu kept appearing

Is there a more efficient way to block the context menu from appearing and prevent the list item from appearing selected??

Upvotes: 0

Views: 269

Answers (1)

buggy_durability
buggy_durability

Reputation: 76

Looks like overriding the onPrepareOptionsMenu method might be of use to you.

I found mention of this in the following question that seems similar to you:

Android: How to enable/disable option menu item on button click?

Upvotes: 0

Related Questions