Android's menu has a variety of implementations, previously written in an Android five popular menus (menu), here is a new menu implementation method: Actionmode. Actionmode is available after Android 3.0, we can use the AppCompat library to make Actionmode compatible with Android 2.1.
Before Android 3.0, we processed the list of long press events often using the context menu,android3.0 later, we had a new option: Actionmode. The left effect is the context menu right effect is actionmode.
Android developers should be familiar with the context menu, andcontext menu is a view hovering over an action item. Actionmode is temporarily occupying the Actionbar position. The implementation of Actionmode is given below.
Using Actionmode requires two steps:
1, realize Actionmode.callback interface, and deal with the life cycle of Actionmode, Actionmode life cycle such as:
PrivateActionmode.callback Mcallback =NewActionmode.callback () {@Override Public BooleanOnprepareactionmode (actionmode Mode, menu menu) {return false; } @Override Public voidOndestroyactionmode (Actionmode mode) {//TODO auto-generated Method Stub} @Override Public BooleanOncreateactionmode (actionmode Mode, menu menu) {menuinflater inflater=Mode.getmenuinflater (); Inflater.inflate (R.menu.actionmode, menu); return true; } @Override Public Booleanonactionitemclicked (actionmode mode, MenuItem item) {BooleanRET =false; if(Item.getitemid () = =r.id.actionmode_cancel) {mode.finish (); RET=true; } returnret; } };
2 . When the event is triggered, call the Startactionmode () method.
Someview.setonlongclicklistener (NewView.onlongclicklistener () {//called when the user long-clicks on Someview Public BooleanOnlongclick (view view) {if(Mactionmode! =NULL) { return false; } //Start the CAB using the Actionmode.callback defined aboveMactionmode =getactivity (). Startactionmode (Mactionmodecallback); View.setselected (true); return true; } });
/*** @author Zhang Xingye* HTTP://BLOG.CSDN.NET/XYZ_LMN* iOS starter Group:83702688
* Android Development Advanced Group:241395671 * My Sina Weibo:
@ Zhang Xingye tbow */
Reference:
Http://developer.android.com/reference/android/view/ActionMode.Callback.html
Http://developer.android.com/guide/topics/ui/menus.html
Go Android New Menu Implementation--actionmode