Android Implementation pop-up menu effect when long press QQ list

Source: Internet
Author: User

This is just a simple principle, simple function, interested readers can optimize their own changes


Layout file

<?xml version= "1.0"  encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Match_parent "     android:layout_height= "match_parent"     android:orientation= "vertical"   >    <listview        android:id= "@+id/data_ List "        android:layout_width=" Match_parent "         android:divider= "@android: Color/black"          android:dividerheight= "0.65dip"         android:layout_height= " Wrap_content " >    </ListView>    <LinearLayout          android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"         android:id= "@+id/data_empty_li"          android:orientation= "Vertical"          android:gravity= "Center"         >                  <ImageView             android:layout_margintop= "100dip"          android:id= "@+id/imageview1"          android:layout_width= "Wrap_content"         android:layout_height = "Wrap_content"         android:src= "@drawable/ic_launcher"  />     <textview        android:id= "@+id/textView1 "    &nbSp;   android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android:layout_margintop= " 20dip "        android:text=" Click to refresh " />             </linearlayout></linearlayout >

Key Code

Public class windowtipactivity  extends activity implements onclicklistener , onitemlongclicklistener, ontouchlistener{private listview mlistview;private  viewgroup mnodatapanel;private final list<string> datalist = new  Arraylist<string> ();p rivate point praw = new point () @Overrideprotected   Void oncreate (bundle savedinstancestate)  {super.oncreate (savedinstancestate); SetContentView ( R.layout.wintip_list_layout);mlistview =  (ListView)  findviewbyid (r.id.data_list); MNoDataPanel  =  (ViewGroup)  findviewbyid (R.id.data_empty_li); Mlistview.setadapter (new arrayadapter< String> (this,android. r.layout.simple_list_item_1 , datalist)); Mlistview.setemptyview (Mnodatapanel); Mnodatapanel.setonclicklistener (This), Mlistview.setonitemlongclicklistener (this); Mlistview.setontouchlistener ( this);} @Overrideprotected &NBsp;void ondestroy ()  {super.ondestroy ();} @Overridepublic  void onclick (view arg0) {if (Datalist.size () >=20) {return;} for  (int i = 1; i <=20; i++)  {datalist.add ("menu-option (" +i+ ")");} arrayadapter adapter =  (Arrayadapter)  mlistview.getadapter (); Adapter.notifyDataSetChanged ( );} @Overridepublic  boolean onitemlongclick (adapterview<?> parent, view v, int  position,long itemid) {    linearlayout layout = new  LinearLayout (this);      layout.setgravity (Gravity.center);         layout.setbackgroundcolor (Color.gray);           textview tv = new textview (This);           tv.setlayoutparams (New layoutparams (LAYOUTPARAMS.WRAP_CONTENT,&NBSP;LAYOUTPARAMS.WRap_content));           tv.settext (DataList.get (position));           tv.settextcolor (Color.WHITE);         tv.setsingleline (True);         Layout.addview (TV);                   paint p = new paint (Paint.anti_alias_flag);                 popupwindow popupwindow =  new popupwindow (Layout,200,v.getheight ());                     popupwindow.setfocusable (True);           popupwindow.setoutsidetouchable (True);           pOpupwindow.setbackgrounddrawable (New bitmapdrawable ());                     int[] location = new  int[2];          v.getlocationonscreen (location);                      popupwindow.showatlocation (V, gravity.no_gravity,parent.getwidth ()/2-100 , location [1]-popupwindow.getheight ());           return false;} @Overridepublic  boolean ontouch (view v, motionevent event) {if (event.getaction () = = Motionevent.action_down) {int x =  (int)  event.getrawx ();int y =  (int)   Event.getrawy ();p raw.set (x, y); LOG.D ("OnTouch",  "x=" +x+ " , y=" +y);} Return false;}}

The effect is as follows


Android Implementation pop-up menu effect when long press QQ list

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.