Activity_main.xml
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "Match_ Parent "android:id=" @+id/rl_root "tools:context=" com.example.popupwindowvslistview.MainActivity "> <relativ Elayout android:id= "@+id/rl_common_default" android:layout_width= "Fill_parent" android:layout_height= "48DP" android:background= "@drawable/top_bg" > <imageview android:id= "@+id/iv_common_more" Android:layout_width= "34DP" android:layout_height= "34DP" android:padding= "5DP" Android:layout_alignparentright= "true" android:layout_centervertical= "true" Android:layout_marginri ght= "15DP" android:src= "@drawable/top_more_n" android:visibility= "visible"/> </relativelay Out></relativelayout>
Mainactivity
Package Com.example.popupwindowvslistview;import Com.example.popupwindowvslistview.CommonPopuWindow.AnimStyle; Import Android.app.activity;import android.os.bundle;import Android.view.view;import Android.view.Window;import Android.widget.imageview;import Android.widget.relativelayout;import Android.widget.toast;public Class Mainactivity extends Activity {private relativelayout rl_common_default;private ImageView iv_common_more;private Commonpopuwindow msmsmorepopup; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Initview (); Initpopup (); Iv_common_more.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick ( View v) {if (msmsmorepopup.isshowing ()) {Msmsmorepopup.dismiss ();//Close}msmsmorepopup.show (Animstyle.rightanim);}});} private void Initpopup () {msmsmorepopup = new Commonpopuwindow (This, r.style.animation,new Commonpopuwindow.itemclickcalLback () {@Overridepublic void callBack (int position) {switch (position) {case 0:toast.maketext (mainactivity.this, " Toast Test Popup empty Record ", Toast.length_short). Show (); Msmsmorepopup.thisdismiss (Animstyle.rightanim); Break;case 1: Toast.maketext (mainactivity.this, "Toast Test popup Backup", Toast.length_short). Show (); Msmsmorepopup.thisdismiss ( Animstyle.rightanim); Break;case 2:toast.maketext (Mainactivity.this, "Toast Test popup import", Toast.length_short). Show (); Msmsmorepopup.thisdismiss (Animstyle.rightanim); Break;case 3:toast.maketext (Mainactivity.this, "Toast Test popup Recharge Payment", Toast.length_short). Show (); Msmsmorepopup.thisdismiss (Animstyle.rightanim); Break;case 4: Msmsmorepopup.thisdismiss (Animstyle.rightanim); Break;case 5:msmsmorepopup.thisdismiss (AnimStyle.RIGHTANIM); Break,}}}, null); Msmsmorepopup.initdata (R.array.messagepopuwindowmore);} private void Initview () {Rl_common_default = (relativelayout) Findviewbyid (r.id.rl_common_default); Iv_common_more = ( ImageView) Findviewbyid (R.id.iv_common_more);}}Commonpopuwindow
Package Com.example.popupwindowvslistview;import Android.annotation.suppresslint;import android.app.Activity; Import Android.content.context;import Android.content.res.resources;import Android.graphics.rect;import Android.graphics.drawable.bitmapdrawable;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.view.window;import Android.view.WindowManager; Import Android.view.animation.animation;import Android.view.animation.animation.animationlistener;import Android.view.animation.scaleanimation;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.ListView; Import Android.widget.popupwindow;import Android.widget.popupwindow.ondismisslistener;import Android.widget.textview;import Android.widget.linearlayout.layoutparams;public class CommonPopuWindow extends Popupwindow implements Animationlistener,ondismisslistener {privateActivity mactivity;private View rootview;private ListView mlistview;private Resources mresources;private DataAdapter Madapter;public enum Animstyle {leftanim, rightanim}private scaleanimation Leftshowanim, RightShowAnim, LeftExitAnim, rightexitanim;private itemclickcallback mcallback;private int animstyle;public Commonpopuwindow (activity activity, int Animstyle,itemclickcallback callBack, String lvwidthtag) {this.mactivity = Activity;this.mresources = Activity.getresources (); this.mcallback = Callback;this.animstyle = Animstyle;init (Lvwidthtag);} @SuppressLint ("Inflateparams") @SuppressWarnings ("deprecation") private void init (String lvwidthtag) {This.rootview = Layoutinflater.from (mactivity). Inflate (r.layout.popupwindow_layout, null); This.mlistview = (ListView) Rootview.findviewbyid (r.id.lv_popup_list); This.setcontentview (Rootview); This.setwidth (LayoutParams.WRAP_ CONTENT); This.setheight (layoutparams.wrap_content); this.setfocusable (true); This.setondismisslistener (this); This.setbackgrounddraWable (New bitmapdrawable ()); This.setanimationstyle (Animstyle); this.setoutsidetouchable (true); This.mListView.setOnItemClickListener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<? > Parent, View view,int position, long ID) {if (mcallback! = null) {mcallback.callback (position);}});} /** * Get Data */public void InitData (int stringarrayid) {string[] arrays = Mresources.getstringarray (Stringarrayid); Madapter = new DataAdapter (arrays); Mlistview.setadapter (Madapter);} /** * Show */public void Show (View anchor, int xoff, int yoff, Animstyle style) {This.showasdropdown (anchor, Xoff, Yoff);p op Upshowalpha (); Showanim (style);} public void Show (Animstyle style) {Rect frame = new Rect (); Mactivity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame), int mmorepopupmargintop = Frame.top + dp2px (mactivity, n); int Mmorepopupmarginright = dp2px (mactivity, +);p opupshowalpha (); This.showatlocation (Mactivity.findviewbyid (R.id.rl_ Root), gravity.right| Gravity.top, MMOREPOPUPMArginright, Mmorepopupmargintop); Showanim (style);} /** * Show animation effect */private void Showanim (Animstyle style) {switch (style) {case Leftanim:if (Leftshowanim = = null) {Leftshowan im = new Scaleanimation (0f, 1f, 0f, 1f,animation.relative_to_self, 0.0f,animation.relative_to_self, 0.0f); Leftshowanim.setduration (+); Leftshowanim.setfillafter (true);} Rootview.startanimation (Leftshowanim); Break;case rightanim:if (Rightshowanim = = null) {Rightshowanim = new Scaleanimation (0f, 1f, 0f, 1f,animation.relative_to_self, 1.0f,animation.relative_to_self, 0.0f); Rightshowanim.setduration (+); Rightshowanim.setfillafter (true);} Rootview.startanimation (Rightshowanim); break;}} /** * Exit animation effect */public void Thisdismiss (Animstyle style) {switch (style) {case Leftanim:if (Leftexitanim = = null) {Leftexit Anim = new Scaleanimation (1f, 0f, 1f, 0f,animation.relative_to_self, 0.0f,animation.relative_to_self, 0.0f); Leftexitanim.setduration (+); Leftexitanim.setfillafter (true); Leftexitanim.setanimationlistener (this);} Rootview.sTartanimation (Leftexitanim); Break;case rightanim:if (Rightexitanim = null) {Rightexitanim = new scaleanimation (1f, 0f, 1f, 0f,animation.relative_to_self, 1.0f,animation.relative_to_self, 0.0f); rightexitanim.setduration (250); Rightexitanim.setfillafter (True); Rightexitanim.setanimationlistener (this);} Rootview.startanimation (Rightexitanim); break;}} @Overridepublic void Onanimationend (Animation Animation) {This.dismiss ();} private void Popupshowalpha () {window window = (Activity) mactivity). GetWindow (); Windowmanager.layoutparams params = window.getattributes ();p Arams.alpha = 0.6f;window.setattributes (params);} private void Popupexitalpha () {window window = (Activity) mactivity). GetWindow (); Windowmanager.layoutparams params = window.getattributes ();p Arams.alpha = 1.0f;window.setattributes (params);} Private class DataAdapter extends Baseadapter {private string[] arrays;class viewholder {TextView dataView;} Public DataAdapter (string[] arrays) {this.arrays = arrays;} @Overridepublic int GetCount () {if (arrays! = null) {return arrays.length;} return 0;} @Overridepublic Object getItem (int position) {if (arrays! = null) {return arrays[position];} return null;} @Overridepublic long Getitemid (int position) {return position;} @SuppressLint ("Inflateparams") @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) { Viewholder viewholder;if (Convertview = = null) {Viewholder = new Viewholder (); Convertview = Layoutinflater.from ( mactivity). Inflate (R.layout.list_item_popupwindow, null); Viewholder.dataview = (TextView) Convertview.findviewbyid (R.id.tv_list_item); Convertview.settag (Viewholder);} else {Viewholder = (Viewholder) Convertview.gettag ();} ViewHolder.dataView.setText (Arrays[position]); return convertview;}} public interface Itemclickcallback {void callBack (int position);} @Overridepublic void Onanimationstart (Animation Animation) {} @Overridepublic void Onanimationrepeat (Animation Animation) {} @Overridepublic void Ondismiss () {Popupexitalpha ();} private int dp2px (Context cOntext, float DP) {final FLOAT scale = context.getresources (). Getdisplaymetrics (). Density;return (int) (DP * scale + 0.5f) ;}}
Popupwindow_layout.xml
<?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 "> <listview android:id= "@+id/lv_popup_list" android:layout_width= "110DP" android:layout_height= "Wrap_content" android:background= "@drawable/layer_popup" android:divider= "@drawable/helper_line" android: Focusableintouchmode= "true" android:footerdividersenabled= "false" android:listselector= "@drawable/ Popupwindow_list_item_text_selector " android:paddingbottom=" 5DP " android:paddingtop=" 5DP " Android:scrollbars= "None"/></linearlayout>
List_item_popupwindow.xml
<?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:o rientation= "vertical" > <textview android:id= "@+id/tv_list_item" android:layout_width= "Fill_ Parent " android:layout_height=" 45DP " android:textsize=" 16DP " android:textcolor=" #111111 " Android:text= "AAA" android:gravity= "center" android:background= "@drawable/popupwindow_list_item_text _selector " /></linearlayout>
Layer_popup.xml
<?xml version= "1.0" encoding= "Utf-8"?> <layer-list xmlns:android= "Http://schemas.android.com/apk/res /android "> <!--Bottom 2DP Shadow- - <item> <shape android:shape=" Rectangle " > <solid android:color= "#d9d9d9"/> <corners android:radius= "5DP"/> </shape> </item> <!--white Top color-- <item android:bottom= "5px" > <shape Android:shape= "Rectangle" > <solid android:color= "#d9d9d9"/> <corners android:radius= "5DP"/ > </shape> </item> </layer-list> <!-- --
Popupwindow_list_item_text_selector.xml
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" true " android:drawable=" @drawable/more_popu_normal "/> <item android:drawable= "@drawable/more_popu_pressed"/></selector>
Out.xml
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <!-- displacement effect fromxdelta= "0%p" refers to the x-axis that is relative to the parent control from the parent control's x=0 out of the start displacement fromydelta= "0%p" refers to the y-axis relative to the parent control from the parent control's y= 0 out start displacement toxdelta= "100%p" refers to the x-axis of the parent control that reaches the x=100 position of the parent control, which is the x-axis at the end of the screen toydelta= "100%p" refers to the y-axis relative to the parent control reaching the parent control y= The 100 position is the x-axis at the end of the screen-- <scale android:duration= "+" android:pivotx= "100%" android: Pivoty= "0%" android:fromxscale= "1.0" android:toxscale= "0.0" android:fromyscale= "1.0" android:toyscale= "0.0"/> </set>
Besides, there's arrays.xml.
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string-array name= "Messagepopuwindowmore" > <item > @string/message_clear</item> <item > @string/back_up</item> < Item > @string/lead_into</item> <item > @string/pay</item> <item > @string/set </item> <item > @string/login_out</item> </string-array></resources>
Color.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <drawable name= "more_popu_pressed" ># d9d9d9</drawable> <drawable name= "More_popu_normal" > #ffffff </drawable></resources>
Styles.xml
<style name= "Animation" > <item name= "android:windowexitanimation" > @anim/out</item></ Style>
Custom Popupwindow+listview+anim