FileDocCategorySizeDatePackage
MenuScenario.javaAPI DocAndroid 5.1 API5085Thu Mar 12 22:22:12 GMT 2015android.view.menu

MenuScenario

public class MenuScenario extends android.app.Activity implements MenuItem.OnMenuItemClickListener
Utility base class for creating various Menu scenarios. Configurable by the number of menu items. Used @link {@link ListScenario} as a reference.

Fields Summary
private Params
mParams
private android.view.Menu
mMenu
private android.view.MenuItem[]
mItems
private boolean[]
mWasItemClicked
Constructors Summary
Methods Summary
private voiddispatchInitParams()

        onInitParams(mParams);
        onParamsChanged();
    
public intfindItemPosition(android.view.MenuItem item)
Finds the position for a given Item.

param
item The item to find.
return
The position, or -1 if not found.

        // Could create reverse mapping, but optimizations aren't important (yet :P)
        for (int i = 0; i < mParams.numItems; i++) {
            if (mItems[i] == item) return i;
        }
        
        return -1;
    
public android.view.MenugetMenu()

        return mMenu;
    
protected android.view.MenuItemonAddMenuItem(android.view.Menu menu, int itemPosition)
Override this to add an item to the menu.

param
itemPosition The position of the item to add (only for your reference).
return
The item that was added to the menu, or null if nothing was added.

        return null;
    
protected voidonCreate(android.os.Bundle icicle)

    
    
        
        super.onCreate(icicle);

        dispatchInitParams();
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        // Safe to hold on to
        mMenu = menu;
        
        if (!mParams.shouldShowMenu) return false;
        
        MenuItem item;
        for (int i = 0; i < mParams.numItems; i++) {
            if ((item = onAddMenuItem(menu, i)) == null) {
                // Add a default item for this position if the subclasses
                // haven't
                CharSequence givenTitle = mParams.itemTitles.get(i);
                item = menu.add(0, 0, 0, (givenTitle != null) ? givenTitle : ("Item " + i));
            }
    
            if (item != null) {
                mItems[i] = item;
                
                if (mParams.listenForClicks) {
                    item.setOnMenuItemClickListener(this);
                }
            }
                
        }
        
        return true;
    
protected voidonInitParams(android.view.menu.MenuScenario$Params params)
Override this to set the parameters for the scenario. Call through to super first.

param
params

    
public booleanonMenuItemClick(android.view.MenuItem item)

        final int position = findItemPosition(item);
        if (position < 0) return false;
        
        mWasItemClicked[position] = true;
        
        return true;
    
public voidonParamsChanged()

        mItems = new MenuItem[mParams.numItems];
        mWasItemClicked = new boolean[mParams.numItems];
    
public booleanonPrepareOptionsMenu(android.view.Menu menu)

        // Safe to hold on to
        mMenu = menu;

        return mParams.shouldShowMenu;
    
public voidsetParams(android.view.menu.MenuScenario$Params params)

        mParams = params;
        onParamsChanged();
    
public booleanwasItemClicked(int position)

        return mWasItemClicked[position];