Contentview Introduction
The context menu inherits the Android.view.Menu, so we can add menu items to the context menu as we do with the Options menu. The main difference between the context menu and the Options menu is that the owner of the Options menu is activity, while the owner of the context menu is the view in the activity. Each activity has and has only one options Menu, which serves the entire activity. While an activity often has multiple view, not every view has a context menu, which requires us to display the Registerforcontextmenu (view view) to specify.
Although the owner of the context menu is View, the build context menu is oncreatecontextmenu through activity (ContextMenu menu, view V, contextmenu.contextmenuinfo Menuinfo) method.
Another notable is the contextmenuinfo in which the object of this class is passed in Oncreatecontextmenu (ContextMenu menu, View V, contextmenu.contextmenuinfo Menuinfo) method, then what is the use of it? Sometimes, a view element needs to pass some information to the context menu, such as the ID of the view corresponding to the DB record, and this will use Contextmenuinfo. The view that needs to pass additional information needs to override the Getcontextmenuinfo () method to return a Contextmenuinfo implementation class object with data.
The first method of implementation
1. First use the Registerforcontextmenu method to register the Contentview for view.
2. The replication Oncreatecontextmenu and oncontextitemselected methods to implement the menu and click events.
code example:
//Register context menuPublic class niceactivity extends listactivity This. Registerforcontextmenu (Getlistview ());//Generate context menu@OverridePublicvoid Oncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {log.v (TAG,"Populate context menu");//Set context menu titleMenu.setheadertitle ("File Operations");//Add context menu itemMenu.add (0,1, Menu.none,"Nice"); Menu.add (0,2, Menu.none,"Hello"); Menu.add (0,3, Menu.none,"World"); Menu.add (0,4, Menu.none,"Liupeng");}//Response context menu item@OverridePublicboolean oncontextitemselected (MenuItem item) {//Get the item information that is currently selectedAdaptercontextmenuinfo Menuinfo = (adaptercontextmenuinfo) item.getmenuinfo (); LOG.V (TAG,"Context Item seleted id="+ menuinfo.id); Switch (Item.getitemid ()) {case1://Do something Break; CASE2://Do something Break; CASE3://Do something Break; CASE4://Do something Break;default: returnsuper.oncontextitemselected (item); } returntrue;}
The second way
1. Set the context menu to listen for events, and implement the Listener interface, the code is as follows:
private GridView gridmyscrawl; Gridmyscrawl.setoncreatecontextmenulistener (new Oncreatecontextmenulistener () {public void onCr Eatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {menu.add (0 , 0 , 0 , R. string . Share); Menu.add (0 , 1 , 0 , R.string . Delete); Menu.add (0 , 2 , 0 , R.string . Cancel); } });
2. Replication Oncontextitemselected method, note that the method of the replication and the above interface is corresponding.
@Override PublicBoolean oncontextitemselected (MenuItem item) {Adapterview.adaptercontextmenuinfo info = (ADAPTERVIEW.ADAPTERC Ontextmenuinfo) Item.getmenuinfo (); String filelocation =NULL;Switch(Item.getitemid ()) { Case 0://cpusctech,shareString extraPath1 = Environment.getexternalstoragedirectory (). toString (); String extraPath2 ="Assets/artshow/papercut arts/";if(Filepage = =1)//scrawl{filelocation = extraPath1 + Cutworks + fileName; }Else if(Filepage = =2{filelocation = extraPath1 + Worldworks + fileName; } Intent shareintent=NewIntent (Intent.action_send); File file1=NewFile (filelocation); Shareintent.putextra (Intent.extra_stream, Uri.fromfile (file1)); Shareintent.putextra (Intent.extra_text, R.string. FINISHTIPS4); Shareintent.settype ("Image/jpeg"); StartActivity (Intent.createchooser (Shareintent, getString (R.string. sharetips)); Break; Case 1://Delete if(Filepage = =3) Toast.maketext (multigridpage. This,"Kiss!" If I don't look good, tell me, do I have to change my%>_<%?, Toast.length_short). Show ();Else{//Get the picture on the SD card storage pathString Extrapath = Environment.getexternalstoragedirectory (). toString ();if(Filepage = =1)//scrawl{filelocation = Extrapath + Cutworks + fileName; }Else if(Filepage = =2{filelocation = Extrapath + Worldworks + fileName; }ElseToast.maketext (multigridpage. This,"Empty Location", Toast.length_short). Show ();//delete data on SD cardFile File =NewFile (filelocation);//system.out.println ("Delete:" + filelocation); if(!file.exists ()) {Toast.maketext (multigridpage). This-3.string. Deletetips, Toast.length_short). Show (); } file.Delete();//rebind data if(Filepage = =1)//scrawl{//imagesource _is = new ImageSource (this); is=NewImageSource ( This); Viewpager.removeview (Gridmyscrawl); Myscrawladapter =NewGridimageadapter ( This, DM, getString (R.string. My_scrawl), is); Gridmyscrawl.setadapter (Myscrawladapter); }Else if(Filepage = =2) {//imagesource _is = new ImageSource (this); is=NewImageSource ( This); Viewpager.removeview (Gridmypapercut); Mypapercutadapter =NewGridimageadapter ( This, DM, getString (R.string. My_papercut), is);//Create a new grid picture adaptation, in fact, with the ListView control more appropriateGridmypapercut.setadapter (Mypapercutadapter);//Set Adaptive} } Break; Case 2://Cancel Break;default: Break; }return Super. oncontextitemselected (item); }
As follows:
Android context Menu Contentview detailed