FileDocCategorySizeDatePackage
MenuScenario.javaAPI DocAndroid 1.5 API6407Wed May 06 22:42:02 BST 2009com.android.frameworktest.menus

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
private com.android.internal.view.menu.MenuBuilder.MenuAdapter[]
mMenuAdapters
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.ViewgetItemView(int menuType, int position)
Gets the menu item view for a given position.

param
menuType The type of menu.
param
position The position of the item.
return
The menu item view for the given item in the given menu type.

        return getMenuAdapter(menuType).getView(position, null, null);
    
public android.view.MenugetMenu()

        return mMenu;
    
public com.android.internal.view.menu.MenuBuilder.MenuAdaptergetMenuAdapter(int menuType)

see
MenuBuilder#getMenuAdapter(int)

        if (mMenuAdapters[menuType] == null) {
            mMenuAdapters[menuType] = ((MenuBuilder) mMenu).getMenuAdapter(menuType);
        }
        
        return mMenuAdapters[menuType];
    
public android.view.ViewgetMenuView(int menuType)
Gets a menu view. Call this after you're sure it has been shown, otherwise it may not have the proper layout_* attributes set.

param
menuType The type of menu.
return
The MenuView for that type.

        return ((MenuBuilder) mMenu).getMenuView(menuType, null);
    
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(com.android.frameworktest.menus.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(com.android.frameworktest.menus.MenuScenario$Params params)

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

        return mWasItemClicked[position];