简体   繁体   中英

Open another ContextMenu when clicked on first ContextMenu's Item

registerForContextMenu(validate_button);
@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenu.ContextMenuInfo menuInfo) {

        menu.add("It's right");
        menu.add("It's wrong");
    }

    @Override
    public boolean onContextItemSelected(android.view.MenuItem item) {
        //i want to show another Context Menu to select next answer
        return true;
    }

How to show another ContextMenu when I click an item of the first ContextMenu ?.

Its something like a questioner where you select answers from the ContextMenu items, depending on which next ContextMenu items must be Populated

How to show another ContextMenu when i click an Item of the 1st ContextMenu??

I don't know how deep you want the ContextMenu to be but for two levels you could have a boolean flag, to select between the two cases, which will be updated from onContextItemSelected :

boolean flag = true;

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenu.ContextMenuInfo menuInfo) {
    if (flag) {
       menu.add("It's right");
       menu.add("It's wrong");
    } else {
       // the second menu
       menu.add("Ha Ha");
    }
}

Then in the onContexItemSelected callback simply update the flag and post a Runnable to show the ContextMenu again:

@Override
public boolean onContextItemSelected(MenuItem item) {
       if (item.getItemId() == theIdOfItemFromSecondContextMenu) {
        // do stuff
        return true;
    }
    // if you pass at this level, then you need to show the second ContextMenu
    flag = false;
    theView.post(new Runnable() {

        @Override
        public void run() {
            theView.showContextMenu();
        }
    });
    return true;
}

Anyway, I would advise against such code and simply use a custom Dialog (or a DialogFragment to have a backstack for the user to cycle through).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM