Context menu and contextual Action Mode (Learn Android Programming the Big Nerd Ranch Guide)

Source: Internet
Author: User

In the Android programming the Big Nerd Ranch guide Book, explain the implementation of the context menu and add a delete menu.

The context action is rendered as a suspended context menu in the device before the honeycomb release. In the subsequent device, it is recommended to use the context action Bar to render the action of the context. The context action Bar is displayed on top of an activity. The

Runs before the honeycomb version of the program, and some of the context action Bar also appears. That's because it uses a third-party library whose name is Actionbarsherlock.

The context menu that implements the hover

     1, which defines the Delete menu resource file with XML.

     2, implement Oncreatecontextmenu (...) method, which is used to inflate the menu resource file.

  @Override  public  void   Oncreatecontextmenu (ContextMenu menu, View V, Contextmenuinfo menuinfo)
 {getactivity (). Getmenuinflater (). Inflate (R.menu.crime_list_item_context, menu);}  

    3, to register this context menu in Oncreateview.

 listview listview = (ListView) View.findviewbyid (Android.
r.id.list); Registerforcontextmenu (ListView);  

4, finally in the implementation of the oncontextitemselected (MenuItem Item) method, to handle the Delete menu is clicked on the event.

Second, Compatibility context action Bar Menu

1, modify the Oncreateview for the context menu registration.

ListView ListView = (ListView) View.findviewbyid (Android. r.id.list); if (Build.VERSION.SDK_INT < build.version_codes. Honeycomb) {    registerforcontextmenu (listView);} Else {    listview.setchoicemode (listview.choice_mode_multiple_modal);}

Listview.choice_mode_multiple_modal is the ListView multi-select mode.

In the suspended context menu, only one item,activity can be selected at a time after selection is overwritten into the paused state, very inconvenient.

In this contextual Action Bar Menu mode, you can select multiple item at a time.

2. Set up a listener for the ListView. The Abslistview.multichoicemodelistener interface needs to be implemented.

This interface contains a method:

 Public void int position,   longboolean checked)

It responds when item is selected and unchecked.

Multichoicemodelistener also implements another interface, Actionmode.callback, which contains four methods, which are callbacks at different stages of the Actionmode life cycle.

 Public Boolean Oncreateactionmode (actionmode Mode, menu menu)  Public Boolean Onprepareactionmode (actionmode Mode, menu menu)  Public Boolean onactionitemclicked (actionmode mode, MenuItem Item)  Public void Ondestroyactionmode (actionmode mode)

You can inflate the resource file of the menu in the Oncreateactionmode method. You can also set the title at this point in this method.

You can set the event that is triggered when you click the Delete menu item in the Onactionitemclicked method.

Contextual action mode can be implemented not only in the ListView, but also in other view, such as Girdview.

It is also possible if it is neither a ListView nor a girdview. Need to implement an interface, View.onlongclicklistener. When implementing this interface, you can use the

Acitivty.startactionmode (...) method to create an Actionmode instance, the only argument to this method is an instance of the Actionmode.callback interface.

Context menu and contextual Action Mode (Learn Android Programming the Big Nerd Ranch Guide)

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.