Context menu and Contextual Action Mode (learn Android Programming The Big Nerd Ranch Guide ),
In The Android Programming The Big Nerd Ranch Guide, we will explain how to implement The context menu and add a delete menu.
In the devices before the release of Honeycomb, context actions are displayed in the form of a floating context menu. In subsequent devices, we recommend that you use the context action bar to present the context action. The action bar of the context is displayed on the top of an activity.
Some programs running in versions earlier than Honeycomb also have the context action bar. That's because it uses a third-party library named ActionBarSherlock.
1. Implement the suspended context menu
1. Use XML to define the "delete menu" resource file.
2. Implement onCreateContextMenu (.........) To inflate the menu resource file.
@Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { getActivity().getMenuInflater().inflate(R.menu.crime_list_item_context, menu);}3. register the context menu in onCreateView.
ListView listView = (ListView)view.findViewById(android.R.id.list);registerForContextMenu(listView);
4. At last, the onContextItemSelected (MenuItem item) method is implemented to handle the deletion menu clicked events.
2. compatible with the context action bar menu
1. Modify the registration of context menu in onCreateView.
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 multiple choice mode of listview.
In the suspended context menu, only one item can be selected at a time. After the activity is selected, it is overwritten and enters the Paused state, which is inconvenient.
In this contextual action bar menu mode, you can select multiple items at a time.
2. Set a listener for listview. You need to implement the AbsListView. MultiChoiceModeListener interface.
This interface contains a method:
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
It will respond after the item is selected or deselected.
MultiChoiceModeListener also implements the ActionMode. Callback interface, which contains four methods for Callback at different stages of the ActionMode lifecycle.
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)
The resource file of the inflate menu in the onCreateActionMode method. You can also set the title in this method.
You can set the event triggered after you click Delete in the onActionItemClicked method.
Contextual action mode can be implemented not only in ListView, but also in other views, such as GirdView.
It can also be implemented if it is neither a ListView nor a GirdView. You need to implement an interface, View. OnLongClickListener. You can use
Acitivty. startActionMode (.....) Method To create an ActionMode instance. The unique parameter of this method is the instance of the ActionMode. CallBack interface.