Creating contextual menus create context menu

Source: Internet
Author: User

A contextual menu offers actions that affect a specific item or context frame in the UI. You can provide a context menu
For any view, but they are most often used for items inListView,GridView,
Or other view collections in which the user can perform direct actions on each item.

A context menu provides actions that affect a specific project or context framework in the UI. You can provide a context menu for any point of view, but they are usually used for projects in the List View, displaying data tables, or other sets of views, you can perform operations directly on each project.

There
Are two ways to provide contextual actions:

There are two ways to provide related operations:

  • In a floating context menu. A menu appears as a floating list of menu items (similar
    To a dialog) when the user performs a long-click (Press and hold) on a view that declares support for a context menu. users can perform a contextual action on one item at a time.
  • In the contextual action mode. This mode is a system implementationActionModeThat
    DisplaysContextual action barAt the top of the screen with action items that affect the selected item (s ). when this mode is active, users can perform an action on multiple items at once (If your app allows it ).

Note:The contextual action mode is available on Android 3.0 (API level 11) and higher and is the preferred technique for displaying contextual actions when available. if your app supports versions lower than 3.0 then you shoshould fall back
A floating context menu on those devices.

In a floating context menu. A menu is displayed as a floating menu item list (similar to a dialog box). You can execute a long click (pressed and kept) in a view to announce that a context menu is supported. You can execute context actions in a project at a time. In the context action mode. This mode is a system-implemented actionmode. The context operation bar is displayed at the top of the screen and the action project affects the selected project (s ). When this mode is activated, You can execute an action in multiple projects at one time (if your application allows it ).
Note: The context action mode is available when Android 3.0 (API level 11) and higher and is the preferred technology to display related operations. If your application supports version earlier than 3.0, you should roll back to a floating context menu on those devices.


Creating a floating context menu to create a floating context menu

To provide a floating context menu:

  1. RegisterViewTo which
    The context menu shoshould be associated by callingregisterForContextMenu()And
    Pass itView.

    If your activity usesListViewOrGridViewAnd
    You want each item to provide the same context menu, register all items for a context menu by passingListViewOrGridViewToregisterForContextMenu().

  2. ImplementonCreateContextMenu()Method
    In yourActivityOrFragment.

    When the registered view es a long-Click Event, the system callyouronCreateContextMenu()Method.
    This is where you define the menu items, usually by inflating a menu resource. For example:

    @Overridepublic void onCreateContextMenu(ContextMenu menu, View v,                                ContextMenuInfo menuInfo) {    super.onCreateContextMenu(menu, v, menuInfo);    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.context_menu, menu);}

    MenuInflaterAllows
    You to inflate the context menu from a menu resource. The callback method parameters includeViewThat
    The user selected andContextMenu.ContextMenuInfoObject
    That provides additional information about the item selected. If your activity has several views that each provide a different context menu, you might use these parameters to determine which context menu to inflate.

  3. ImplementonContextItemSelected().

    When the user selects a menu item, the system callthis method so you can perform the appropriate action. For example:

    @Overridepublic boolean onContextItemSelected(MenuItem item) {    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();    switch (item.getItemId()) {        case R.id.edit:            editNote(info.id);            return true;        case R.id.delete:            deleteNote(info.id);            return true;        default:            return super.onContextItemSelected(item);    }}

    ThegetItemId()Method
    Queries the ID for the selected menu item, which you shoshould assign to each menu item in XML usingandroid:idAttribute, as shown in the section about defining
    A menu in XML.

    When you successfully handle a menu item, returntrue. If you don't handle the menu item, you should pass the menu item to the superclass (superclass) implementation.
    If your activity has des fragments, the activity has es this callback first. by calling the superclass when unhandled, the system passes the event to the respective callback method in each fragment, one at a time (in the order each fragment was added)trueOrfalseIs
    Returned. (the default implementationActivityAndandroid.app.FragmentReturnfalse,
    So you shoshould always call the superclass (superclass) When unhandled .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.