ContextMenu Introduction:
If a view registers a context menu, a floating menu pops up when you press and hold the view to select the next action.
Implementing this function requires calling Setoncreatecontextmenulistener to register a listener, so what is the difference between registering a long press listener Setonlongclicklistener? The following will be tested in the code, by adding print discovery, the user to a view long press, the first trigger is the Onlongclick function, perform the onlongclick operation, The Oncreatecontextmenulistener function is then judged based on the return value of the Onlongclick, if Onlongclick returns False, The Oncreatecontextmenulistener function continues to execute, and if true, the Oncreatecontextmenulistener function is ignored.
The code is as follows:
public class Mainactivity extends actionbaractivity {ImageView mimageview; Mycontextmenuclicklistener mcontextlistener; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.fragment_main); Mimageview = (ImageView) findViewById ( R.id.image); mcontextlistener = new Mycontextmenuclicklistener (); Mimageview.setoncreatecontextmenulistener ( Mcontextlistener);//testmimageview.setonlongclicklistener (new Onlongclicklistener () {@Overridepublic Boolean Onlongclick (View arg0) {//TODO auto-generated method Stublog.d ("Test", "Onlongclick"); return false;});} Private class Mycontextmenuclicklistener implements Oncreatecontextmenulistener, onmenuitemclicklistener{@ Overridepublic boolean Onmenuitemclick (MenuItem Item) {//TODO auto-generated method Stubswitch (Item.getitemid ()) {case R.ID.OPEN:LOG.D ("Test", "click Open"); Break;case r.id.delete:log.d ("Test", "click Delete"); Break;case R.id.remove: LOG.D ("Test", "click Remove"); Break;default:breaK;} return true;} @Overridepublic void Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) {//TODO auto-generated Method Stublog.d ("Test", "Oncreatecontextmenu"); Menuinflater inflater = Getmenuinflater (); Inflater.inflate (R.menu.context_menu, menu); Layoutinflater layoutinflater = Getlayoutinflater (); View view = (view) layoutinflater.inflate (R.layout.menu_header, null); Menu.setheaderview (view); MenuItem open = Menu.finditem (R.id.open); Open.setonmenuitemclicklistener (Mcontextlistener); MenuItem Delete = Menu.finditem (r.id.delete);d Elete.setonmenuitemclicklistener (Mcontextlistener); MenuItem remove = Menu.finditem (R.id.remove); Remove.setonmenuitemclicklistener (Mcontextlistener);}}}
Context_menu.xml
<?xml version= "1.0" encoding= "UTF-8"?> <menu xmlns:android= "http://schemas.android.com/apk/res/ Android "> <group android:id=" @+id/context_menu "> <item android:id=" @+id/delete " android: title= "Delete"/> <item android:id= "@+id/open" android:title= "open"/> <item android:id= "@ +id/remove " android:title=" Remove "/> </group> </menu>
Menu_header.xml
<?xml version= "1.0" encoding= "UTF-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:layout_height=" wrap_content " android:layout_width=" wrap_content " > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text = "This is a Test" /> </LinearLayout>