In this article, I will share with you a class that encapsulates the Popupwindow implementation pop-up menu and describes its implementation and use.
Due to the need for the interface, Android native pop-up menu has not been able to meet our needs, the custom menu is our only choice, in this article, I will share with you how to implement the pop-up menu using Popupwindow.
1. Package Popmenu of pop-up menu
Popupwindow can be said to be a floating container above the activity, typically used to display a custom view. For example, like the auto-complete input box Autocompletetextview, its cue list is implemented using Popupwindow. The following abstract class Popmenu encapsulates the UI logic for implementing pop-up menus using Popupwindow, but does not include the setting of the interface layout.
/* * DATE:14-6-13 * project:parking lay-by */package cn.irains.access.v2.common;import Android.content.Context;import A Ndroid.graphics.drawable.colordrawable;import Android.view.keyevent;import Android.view.view;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.popupwindow;import java.util.arraylist;/** * Package for pop-up menus. * AUTHOR:MSDX ([email protected]) * time:14-6-13 pm 1:51 */public abstract class Popmenu {/** * context. */Private Context mcontext; /** * Menu item */private arraylist<item> mitemlist; /** * List adapter. */Private arrayadapter<item> madapter; /** * Menu Select Listen. */Private Onitemselectedlistener Mlistener; /** * list. */Private ListView Mlistview; /** * pop-up window. */Private Popupwindow Mpopupwindow; Public Popmenu (Context context) {Mcontext = context; Mitemlist = new ArrayList<Item> (2); View view = Oncreateview (context); View.setfocusableintouchmode (TRUE); Madapter = Oncreateadapter (context, mitemlist); Mlistview = findListView (view); Mlistview.setadapter (Madapter); Mlistview.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onite Mclick (adapterview<?> Parent, view view, int position, long ID) {Item item = Madapter.getitem (Positi ON); if (Mlistener! = null) {mlistener.selected (view, item, position); } Mpopupwindow.dismiss (); } }); View.setonkeylistener (New View.onkeylistener () {@Override public boolean onKey (View v, int keycode, KeyEvent event) {if (keycode = = Keyevent.keycode_menu && mpopupwindow.isshowing ()) { Mpopupwindow.dismiss (); return true; } return false; } }); Mpopupwindow = new Popupwindow (view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, True) ; Mpopupwindow.setbackgrounddrawable (New colordrawable (0x00000000)); }/** * Menu interface view. * * @param context * @return */protected abstract View Oncreateview (context context); /** * The list in the menu interface view. * * @param view * @return */protected abstract ListView findListView (view view); /** * The adapter in the menu list. * * @param context * @param itemList represents all menu items. * @return */protected abstract arrayadapter<item> oncreateadapter (context context, arraylist<item> ite Mlist); /** * Add menu items. * * @param text contents of the Text menu item. * @param ID menu item ID */public void AddItem (String text, int id) {Mitemlist.add (new Item (text, id)); Madapter.notifydatasetchanged (); }/** * Add menu item. * * @param resId menu item text contentThe ID of the resource ID * @param ID of the menu item. */public void AddItem (int resId, int id) {AddItem (mcontext.getstring (resId), id); }/** * is displayed as a drop-down control for the specified view. * * @param the view */public void Showasdropdown (view parent) {Mpopupwindow.showasdropdown (PARE) specified by the parent NT); }/** * hides the menu. */public void Dismiss () {Mpopupwindow.dismiss (); }/** * Set menu selection listener. * * @param listener listener. */public void Setonitemselectedlistener (Onitemselectedlistener listener) {Mlistener = listener; }/** * Whether the current menu is being displayed. * * @return */public boolean isshowing () {return mpopupwindow.isshowing (); }/** * menu item. */public static class Item {public String text; public int id; Public Item (String text, int id) {this.text = text; This.id = ID; } @Override Public String toString () {return text; } } /** * menu item Select Listener Interface. */public static interface Onitemselectedlistener {/** * menu is selected when the callback interface. * * @param view of the selected content. * @param item is selected menu item. * @param position the selected position. */public void selected (view view, item item, int position); }}
There are three abstract methods, the first one is Oncreateview (context context), where it is necessary to implement and return the view of our pop-up menu before it can be loaded into the Popupwindow and displayed.
The second method is findListView (view view). This is because our menu is usually a list and then clicked to select an item in the list, so here we need to return a ListView object to load our menu items.
The third method is Oncreateadapter, which is the adapter for the ListView.
In this class, an inner class item is also encapsulated:
/** * menu item. */public static class Item {public String text; public int id; Public Item (String text, int id) { this.text = text; This.id = ID; } @Override public String toString () { return text; } }
It is used to represent our menu item, text is the message displayed in the menu, the ID represents the menu item ID.
An interface Onitemselectedlistener is also defined in the abstract class, which is the callback interface when a menu item is clicked. For a description of it, see note, which is the most detailed class I have written so far in this blog.
Use of 2.PopMenu
First, inherit the Popmenu and implement the abstract method:
/* * date:14-9-2 * project:access-control-v2 */package cn.irains.access.v2.usermanager;import Android.content.Context ; Import Android.view.layoutinflater;import Android.view.view;import Android.widget.arrayadapter;import Android.widget.listview;import Java.util.arraylist;import Cn.irains.access.v2.r;import cn.irains.access.v2.common.popmenu;/** * AUTHOR:MSDX ([email protected]) * Time:14-9-2 morning 8:56 */public class UserM ENU extends Popmenu {public Usermenu (context context) {super (context); } @Override protected ListView findListView (view view) {return (ListView) View.findviewbyid (R.ID.MENU_LISTVI EW); } @Override Protected View Oncreateview (context context) {View view = Layoutinflater.from (context). Inflate (R . Layout.menu_user, NULL); return view; } @Override protected Arrayadapter<item> oncreateadapter (context context, arraylist<item> items) { return new arrayadapter<item> (context, r.layout.item_Menu_user, items); }}
The width of the ListView, if not written dead, is the default width filled with the parent control, just as the Viewpager default height fills the parent control. If you want the width of the ListView to fit the content, you need to rewrite it. Refer to the previous article (Android development tips--viewpager derived from the 2 classes), the code is as follows:
/* * date:14-9-2 * project:access-control-v2 */package cn.irains.access.v2.common;import android.content.Context; Import Android.util.attributeset;import android.view.view;import android.widget.listview;/** * Width to fit the contents of the ListView. * AUTHOR:MSDX ([email protected]) * time:14-9-2 pm 5:14 */public Class Wrapwidthlistview extends ListView {publi C Wrapwidthlistview (Context context) {super (context); } public Wrapwidthlistview (context context, AttributeSet Attrs) {Super (context, attrs); } public Wrapwidthlistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {int width = 0; for (int i = 0; i < Getchildcount (); i++) {View child = Getchildat (i); Child.measure (Measurespec.makemeasurespec (0, measurespec.unspecified), Heightmeasurespec); int w = child.getmeasuredwidth (); if (w > width) width = w; } Widthmeasurespec = Measurespec.makemeasurespec (width + getpaddingleft () + getpaddingright (), measurespec.exactly) ; Super.onmeasure (Widthmeasurespec, Heightmeasurespec); }}
The layout file for the popup menu is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Wrap_content "android:paddingright=" @dimen/pop_menu_padding " android:orientation= "vertical" android:layout_height= "wrap_content" > <imageview Android Oid:id= "@+id/head" android:src= "@drawable/pop_menu_head" android:layout_gravity= "right" android:layou T_width= "Wrap_content" android:contentdescription= "@null" android:layout_marginright= "18DP" android:l ayout_height= "Wrap_content"/> <cn.irains.access.v2.common.wrapwidthlistview android:id= "@+id/menu_listview "Android:padding=" 6DP "android:focusable=" true "android:layout_width=" Wrap_content "android:b ackground= "@drawable/pop_menu_body" android:cachecolorhint= "@android: Color/transparent" Android:layout_heigh t= "Wrap_content"/></linearlayOut>
One of the ImageView's photos is a black triangular pattern. I'll get it when I send it at the end. The listview background is a black picture.
Next is the item layout, just a textview, the code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:textsize=" @dimen/text_size_large " android:textcolor=" @color/text_choice_selector " android:background= "@drawable/item_choice_selector" android:gravity= "center" Android:layout_ gravity= "center" android:paddingleft= "20DP" android:paddingtop= "6DP" android:singleline= "true" android:paddingbottom= "6DP" android:paddingright= "20DP" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/>
Use the following code:
private static final int user_search = 0; private static final int user_add = 1; Private Usermenu Mmenu; private void Initmenu () {mmenu = new Usermenu (context); Mmenu.additem (R.string.user_search, User_search); Mmenu.additem (R.string.user_add, User_add); Mmenu.setonitemselectedlistener (New Popmenu.onitemselectedlistener () {@Override public void selecte D (view view, Popmenu.item Item, int position) {switch (item.id) {case User_search: StartActivity (New Intent (Getactivity (), usersearchactivity.class)); Break Case User_add:startactivity (New Intent (Getactivity (), useraddactivity.class)); Break } } }); }
The menu code is initialized in the activity's OnCreate or fragment Oncreateview, and then called Mmenu.showasdropdown (view) when it needs to be displayed; It is displayed as a drop-down menu for view. The effect is as follows:
Android Development Tips-using Popupwindow to implement pop-up menus