Go Android New Menu Implementation--actionmode

Source: Internet
Author: User

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

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.