public interface

MenuPresenter

 androidx.appcompat.view.menu.MenuPresenter

Subclasses:

ListMenuPresenter, BaseMenuPresenter

Gradle dependencies

compile group: 'androidx.appcompat', name: 'appcompat', version: '1.7.0'

  • groupId: androidx.appcompat
  • artifactId: appcompat
  • version: 1.7.0

Artifact androidx.appcompat:appcompat:1.7.0 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.appcompat:appcompat com.android.support:appcompat-v7

Androidx class mapping:

androidx.appcompat.view.menu.MenuPresenter android.support.v7.view.menu.MenuPresenter

Overview

A MenuPresenter is responsible for building views for a Menu object. It takes over some responsibility from the old style monolithic MenuBuilder class.

Summary

Methods
public booleancollapseItemActionView(MenuBuilder menu, MenuItemImpl item)

Called when a menu item with a collapsible action view should collapse its action view.

public booleanexpandItemActionView(MenuBuilder menu, MenuItemImpl item)

Called when a menu item with a collapsible action view should expand its action view.

public booleanflagActionItems()

Called by Menu implementations to flag items that will be shown as actions.

public intgetId()

Returns an ID for determining how to save/restore instance state.

public MenuViewgetMenuView(ViewGroup root)

Retrieve a MenuView to display the menu specified in MenuPresenter.initForMenu(Context, MenuBuilder).

public voidinitForMenu(Context context, MenuBuilder menu)

Initializes this presenter for the given context and menu.

public voidonCloseMenu(MenuBuilder menu, boolean allMenusAreClosing)

Called by Menu implementations to indicate that a menu or submenu is closing.

public voidonRestoreInstanceState(Parcelable state)

Supplies the previously saved instance state to be restored.

public ParcelableonSaveInstanceState()

Returns a Parcelable describing the current state of the presenter.

public booleanonSubMenuSelected(SubMenuBuilder subMenu)

Called by Menu implementations to indicate that a submenu item has been selected.

public voidsetCallback(MenuPresenter.Callback cb)

Set a callback object that will be notified of menu events related to this specific presentation.

public voidupdateMenuView(boolean cleared)

Update the menu UI in response to a change.

Methods

public void initForMenu(Context context, MenuBuilder menu)

Initializes this presenter for the given context and menu.

This method is called by MenuBuilder when a presenter is added. See MenuBuilder.addMenuPresenter(MenuPresenter).

Parameters:

context: the context for this presenter; used for view creation and resource management, must be non-null
menu: the menu to host, or null to clear the hosted menu

public MenuView getMenuView(ViewGroup root)

Retrieve a MenuView to display the menu specified in MenuPresenter.initForMenu(Context, MenuBuilder).

Parameters:

root: Intended parent of the MenuView.

Returns:

A freshly created MenuView.

public void updateMenuView(boolean cleared)

Update the menu UI in response to a change. Called by MenuBuilder during the normal course of operation.

Parameters:

cleared: true if the menu was entirely cleared

public void setCallback(MenuPresenter.Callback cb)

Set a callback object that will be notified of menu events related to this specific presentation.

Parameters:

cb: Callback that will be notified of future events

public boolean onSubMenuSelected(SubMenuBuilder subMenu)

Called by Menu implementations to indicate that a submenu item has been selected. An active Callback should be notified, and if applicable the presenter should present the submenu.

Parameters:

subMenu: SubMenu being opened

Returns:

true if the the event was handled, false otherwise.

public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing)

Called by Menu implementations to indicate that a menu or submenu is closing. Presenter implementations should close the representation of the menu indicated as necessary and notify a registered callback.

Parameters:

menu: the menu or submenu that is closing
allMenusAreClosing: true if all displayed menus and submenus are closing, false if only the specified menu is closing

public boolean flagActionItems()

Called by Menu implementations to flag items that will be shown as actions.

Returns:

true if this presenter changed the action status of any items.

public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item)

Called when a menu item with a collapsible action view should expand its action view.

Parameters:

menu: Menu containing the item to be expanded
item: Item to be expanded

Returns:

true if this presenter expanded the action view, false otherwise.

public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item)

Called when a menu item with a collapsible action view should collapse its action view.

Parameters:

menu: Menu containing the item to be collapsed
item: Item to be collapsed

Returns:

true if this presenter collapsed the action view, false otherwise.

public int getId()

Returns an ID for determining how to save/restore instance state.

Returns:

a valid ID value.

public Parcelable onSaveInstanceState()

Returns a Parcelable describing the current state of the presenter. It will be passed to the MenuPresenter.onRestoreInstanceState(Parcelable) method of the presenter sharing the same ID later.

Returns:

The saved instance state

public void onRestoreInstanceState(Parcelable state)

Supplies the previously saved instance state to be restored.

Parameters:

state: The previously saved instance state

Source

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.appcompat.view.menu;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX;

import android.content.Context;
import android.os.Parcelable;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;

/**
 * A MenuPresenter is responsible for building views for a Menu object. It takes over some
 * responsibility from the old style monolithic MenuBuilder class.
 *
 */
@RestrictTo(LIBRARY_GROUP_PREFIX)
public interface MenuPresenter {

    /**
     * Called by menu implementation to notify another component of open/close events.
     */
    interface Callback {
        /**
         * Called when a menu is closing.
         * @param menu
         * @param allMenusAreClosing
         */
        void onCloseMenu(@NonNull MenuBuilder menu, boolean allMenusAreClosing);

        /**
         * Called when a submenu opens. Useful for notifying the application
         * of menu state so that it does not attempt to hide the action bar
         * while a submenu is open or similar.
         *
         * @param subMenu Submenu currently being opened
         * @return true if the Callback will handle presenting the submenu, false if
         *         the presenter should attempt to do so.
         */
        boolean onOpenSubMenu(@NonNull MenuBuilder subMenu);
    }

    /**
     * Initializes this presenter for the given context and menu.
     * <p>
     * This method is called by MenuBuilder when a presenter is added. See
     * {@link MenuBuilder#addMenuPresenter(MenuPresenter)}.
     *
     * @param context the context for this presenter; used for view creation
     *                and resource management, must be non-{@code null}
     * @param menu the menu to host, or {@code null} to clear the hosted menu
     */
    void initForMenu(Context context, MenuBuilder menu);

    /**
     * Retrieve a MenuView to display the menu specified in
     * {@link #initForMenu(Context, MenuBuilder)}.
     *
     * @param root Intended parent of the MenuView.
     * @return A freshly created MenuView.
     */
    MenuView getMenuView(ViewGroup root);

    /**
     * Update the menu UI in response to a change. Called by
     * MenuBuilder during the normal course of operation.
     *
     * @param cleared true if the menu was entirely cleared
     */
    void updateMenuView(boolean cleared);

    /**
     * Set a callback object that will be notified of menu events
     * related to this specific presentation.
     * @param cb Callback that will be notified of future events
     */
    void setCallback(Callback cb);

    /**
     * Called by Menu implementations to indicate that a submenu item
     * has been selected. An active Callback should be notified, and
     * if applicable the presenter should present the submenu.
     *
     * @param subMenu SubMenu being opened
     * @return true if the the event was handled, false otherwise.
     */
    boolean onSubMenuSelected(SubMenuBuilder subMenu);

    /**
     * Called by Menu implementations to indicate that a menu or submenu is
     * closing. Presenter implementations should close the representation
     * of the menu indicated as necessary and notify a registered callback.
     *
     * @param menu the menu or submenu that is closing
     * @param allMenusAreClosing {@code true} if all displayed menus and
     *                           submenus are closing, {@code false} if only
     *                           the specified menu is closing
     */
    void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing);

    /**
     * Called by Menu implementations to flag items that will be shown as actions.
     * @return true if this presenter changed the action status of any items.
     */
    boolean flagActionItems();

    /**
     * Called when a menu item with a collapsible action view should expand its action view.
     *
     * @param menu Menu containing the item to be expanded
     * @param item Item to be expanded
     * @return true if this presenter expanded the action view, false otherwise.
     */
    boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item);

    /**
     * Called when a menu item with a collapsible action view should collapse its action view.
     *
     * @param menu Menu containing the item to be collapsed
     * @param item Item to be collapsed
     * @return true if this presenter collapsed the action view, false otherwise.
     */
    boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item);

    /**
     * Returns an ID for determining how to save/restore instance state.
     * @return a valid ID value.
     */
    int getId();

    /**
     * Returns a Parcelable describing the current state of the presenter.
     * It will be passed to the {@link #onRestoreInstanceState(Parcelable)}
     * method of the presenter sharing the same ID later.
     * @return The saved instance state
     */
    Parcelable onSaveInstanceState();

    /**
     * Supplies the previously saved instance state to be restored.
     * @param state The previously saved instance state
     */
    void onRestoreInstanceState(Parcelable state);
}