Android Long Press ListView each item bottom popup menu

Source: Internet
Author: User

Android interface, there is a function is to click on each item of the ListView, below the popup two menu, you can click to enter another interface, this feature can use open source project Expandablelistview, it can be implemented, but found that the import of code too much, appear very bloated , after master guidance, I use the following method, the steps are as follows:

1. Create a new ListView, you need to scrollview on the outer layer, but using ScrollView will cause the ListView to have a height of only one item, need to use a custom ListView, the code is as follows:

Package Allone.verbank.apad.client.component.fixedlistview;import Android.content.context;import Android.util.attributeset;import Android.view.view.measurespec;import Android.widget.listview;public Class Pricelistviewscroll extends ListView {public Pricelistviewscroll (context context) {super (context);} Public Pricelistviewscroll (context context, AttributeSet Attrs) {Super (context, attrs);} Public Pricelistviewscroll (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} @Overridepublic void onmeasure (int widthmeasurespec, int heightmeasurespec) {int expandspec = Measurespec.makemeasurespec (Integer.max_value >> 2,measurespec.at_most); Super.onmeasure (WidthMeasureSpec, EXPANDSPEC);}}
The interface file for the ListView is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" @color/backgr Ound "android:orientation=" vertical "> <textview android:layout_width=" fill_parent "Android:layo ut_height= "Wrap_content" android:gravity= "Center_horizontal" android:text= "* * *" android:textcolor= "@c Olor/white "android:textsize=" 16sp "/> <scrollview android:id=" @+id/price_scroll "Android:la        Yout_width= "Fill_parent" android:layout_height= "fill_parent" android:background= "@color/background" > <allone.verbank.apad.client.component.fixedlistview.pricelistviewscroll android:id= "@+id/price_quote_lis T "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:divider = "@color/white" > </allOne.verbank.apad.client.component.fixedlistview.pricelistviewscroll> </scrollview></linearlayout >
2. Each item below the menu, in fact originally is a beginning to hide a view, have a certain height, prevent the menu, the menu is to use Popuwindow to pop up, the item's XML file is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/    Android "android:id=" @+id/below_layout "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " Android:layout_margin= "10DP" android:background= "@color/background" android:orientation= "vertical" > <line Arlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:layout_margi            N= "5DP" android:orientation= "vertical" > <linearlayout android:layout_width= "fill_parent"                android:layout_height= "wrap_content" android:orientation= "Horizontal" > <textview Android:id= "@+id/price_quote_inst" android:layout_width= "Match_parent" Android:                layout_height= "Wrap_content" android:layout_weight= "0.98" android:gravity= "right"     Android:singleline= "true"           android:text= "Wyb/test" android:textcolor= "@color/white" android:textsize= "20DP"        > </TextView> </LinearLayout> <view android:layout_width= "Fill_parent" android:layout_height= "1.2px" android:layout_marginleft= "14DP" android:layout_marginright= "14DP" an droid:layout_margintop= "5DP" android:background= "@color/white"/> <textview android:id= "@+id/hidepr Icemenu "android:layout_width=" fill_parent "android:layout_height=" 30DP "android:visibility=" Gone "/& gt;<!--It is very critical that the menu bounces out when it shows that there is no menu when the--></linearlayout> is hidden

3. The next step is in the code, when long press the ListView to display Hidepricemenu this textview, in order to pop-up menu, the menu is to use Popuwindow to display, the following key code:

List.setonitemlongclicklistener (New Onitemlongclicklistener () {@Overridepublic Boolean onitemlongclick (Adapterview <?> arg0, View sourview,final int arg2, long arg3) {Hidepricemenu = (TextView) Sourview.findviewbyid (r.id.hideprice menu); hidepricemenu.setvisibility (view.visible); String selectintrument = Maps.get (arg2). Get (Price_tc_inst). toString (); Buyselllongclickaction (Sourview, Selectintrument, Hidepricemenu); return true;}); private void Buyselllongclickaction (final View inner,final String instname, final TextView hide_pricemenu_copy) { Staticcontext.trade_inst = Instname;if (PopupMenu! = null) {Popupmenu.destroypopupmenu ();} Surfaced Menu PopupMenu = Popupmenufactory.createpopupwindow (getselfactivity (), inner, Ipopupmenu.menu_id_price, instname); Popupmenu.showpopupmenu ();p Opupmenu.createpupupwindow (). Setondismisslistener (New Popupwindow.ondismisslistener ( {@Overridepublic void Ondismiss () {hide_pricemenu_copy.setvisibility (View.gone);}}); /When it is the last line, the popup menu will be hidden, where the scroll is moved up Handler.post (nEW Runnable () {@Overridepublic void run () {if (price_scroll.getscrolly () > +) {price_scroll.scrollto (0, Price_scroll . getscrolly () + 200);}});}


The code to create the Popuwindow is as follows:

Package Allone.verbank.apad.client.component.menu.impl;import Java.util.arraylist;import Java.util.HashMap;import Java.util.map;import Allone.verbank.apad.client.r;import Allone.verbank.apad.client.staticcontext;import Allone.verbank.apad.client.component.menu.ipopupmenu;import Allone.verbank.apad.client.doc.doccaptain;import Allone.verbank.apad.client.doc.ibundlecommdata;import Allone.verbank.apad.client.event.istationeventname;import Allone.verbank.apad.client.event.stationeventcaptain;import Allone.verbank.apad.client.event.StationEventData; Import Allone.verbank.apad.client.frame.main.mainactivity;import Android.annotation.suppresslint;import Android.app.activity;import Android.graphics.drawable.colordrawable;import Android.view.motionevent;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.view.viewgroup.layoutparams;import Android.view.windowmanager;import Android.widget.BaseAdapter;import Android.widget.button;import andRoid.widget.listview;import Android.widget.popupwindow;public class Pricetradepopupmenu implements IPopupMenu { Private arraylist<map<string, integer>> dataMap = new arraylist<map<string, integer>> (); public static final String table_columnid_ordertrade = "Ordertrade";p ublic static final string table_columnid_ pricewarning = "pricewarning";p rivate ListView listview;private popupwindow popupmenu;private mainactivity activity; Private View Sourceview;private String instrument;public pricetradepopupmenu (activity activity, View Sourceview, Object Data) {this.activity = (mainactivity) Activity;this.sourceview = Sourceview;instrument = Data.tostring (); initComponent ();} private void InitComponent () {View view = Activity.getlayoutinflater (). Inflate (R.layout.popup_price_menu, NULL); ListView = (ListView) View.findviewbyid (R.id.price_menu_list); Addrowdata (); Pricetradeadapter listadapter = new Pricetradeadapter () Listview.setadapter (listadapter)//Create Popup//window contents layoutleft , which contains aA listview//window width is the same as tvleft windowmanager wm = Activity.getwindowmanager (); int width = Wm.getdefaultdisplay (). GetWidth () ;//Width-//sourceview.getwidth () PopupMenu = new Popupwindow (view, Sourceview.getwidth (), layoutparams.wrap_content) ; Colordrawable cd = new colordrawable (r.color.white);p opupmenu.setbackgrounddrawable (CD);// Popupmenu.setanimationstyle (R.style.animationfade);p opupmenu.update ();p Opupmenu.setinputmethodmode ( popupwindow.input_method_needed);p opupmenu.settouchable (true); Set Popupwindow to click Popupmenu.setoutsidetouchable (TRUE); Set Popupwindow external clickable popupmenu.setfocusable (true); Gets the focus popupmenu.settouchinterceptor (new View.ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent Event) {//If you click outside of Popupwindow, Popupwindow will also disappear Stationeventcaptain.getinstance (). Fireeventdatachange (New Stationeventdata (istationeventname.event_name_price_menu_change,instrument)); if (event.getaction () = = Motionevent.action_outside) {Popupmenu.dismiss (); return true;} return false;}}); private void Addrowdata () {Datamap.add (Addrowitemdata ());} Private hashmap<string, integer> Addrowitemdata () {hashmap<string, integer> maptemp = new HashMap<String , integer> (); Maptemp.put (Table_columnid_ordertrade,ibundlecommdata.menu_price_ordertrade); MapTemp.put (TABLE_ columnid_pricewarning,ibundlecommdata.menu_price_pricewarning); return maptemp;} @Overridepublic Popupwindow Createpupupwindow () {return popupmenu;} @Overridepublic void Destroypopupmenu () {if (PopupMenu! = null && popupmenu.isshowing ()) {Popupmenu.dismiss ()}} @SuppressLint ("Newapi") @Overridepublic void ShowPopupMenu () {if (PopupMenu! = null && popupmenu.isshowing ()) { Popupmenu.dismiss ();} else {Popupmenu.showasdropdown (Sourceview, 10,-50 + (int) sourceview.getrotationy ());}} public class Pricetradeadapter extends Baseadapter {@Overridepublic int getcount () {return datamap.size ();} @Overridepublic Object getItem (int position) {return position;} @Overridepublic long Getitemid (int position) {Return Position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder Viewholder = null;if (convertvi EW = = NULL) {Viewholder = new Viewholder (); Convertview = Activity.getlayoutinflater (). Inflate (R.layout.popup_price_ Menu_item, null); Viewholder.ordertradeview = (Button) Convertview.findviewbyid (R.id.price_menu_item_ordertrade); ViewHolder.orderTradeView.setOnClickListener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if ( PopupMenu! = null) {popupmenu.setanimationstyle (0);p Opupmenu.dismiss ();}}); Convertview.settag (Viewholder);} else {Viewholder = (Viewholder) Convertview.gettag ();} int ordertrade = Integer.parseint (datamap.get (position). Get (Table_columnid_ordertrade). toString ()); ViewHolder.orderTradeView.setText (Ordertrade = = Ibundlecommdata.menu_price_ordertrade? activity.getstring ( R.string.pricetradepopupmenu_order): ""); int pricewarning = Integer.parseint (datamap.get (position). Get (Table_ columnid_pricewarning). toString ()); viewholder.pricEwarningview.settext (pricewarning = = ibundlecommdata.menu_price_pricewarning? activity.getstring ( r.string.pricetradepopupmenu_pricewarnning): ""); return Convertview;} Private class Viewholder {Button ordertradeview; Button Pricewarningview;}}}

4. In this way, when the last item, the popup menu will appear in the bottom of the ListView, the position is obscured, so need to front scrollview, judge the height below, and then to move the ListView, using Scrollto method, The following code:

When it is the last line, the popup menu will be hidden, using scroll to move up the IF (price_scroll.getscrolly () >) {price_scroll.scrollto (0, Price_ Scroll.getscrolly () + 200);} if (price_scroll.getscrolly () = = 0) {price_scroll.scrollto (0, price_scroll.getscrolly () + 50);}

At this point can complete the function, some code omitted, because it is the company, there are problems can communicate!!!

Android Long Press ListView each item bottom popup menu

Related Article

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.