Custom PopupWindow + SimpleAdapter + Anim, custom simpleadapter
See http://blog.csdn.net/u013210620/article/details/46011945
MainActivity
Package com. example. mypopupwindow; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. content. context; import android. graphics. rect; import android. graphics. drawable. bitmapDrawable; import android. OS. bundle; import android. view. gravity; import android. view. view; import android. view. view. onClickListener; import android. vi Ew. 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. imageView; import android. widget. linearLayout. layoutParams; import android. widget. listView; import android. Widget. popupWindow; import android. widget. simpleAdapter; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private PopupWindow msmorepopup; private View smsMorePopuLayout; List <Map <String, String> smsMoreList; private ImageView iv_common_more; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindow Feature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); iv_common_more = (ImageView) findViewById (R. id. iv_common_more); iv_common_more.setOnClickListener (this); initsmorepopudata (); initPopup (); mSmsMorePopup. setOnDismissListener (new PopupWindow. onDismissListener () {@ Overridepublic void onDismiss () {popupExitAlpha () ;}});} private void initsmorepopudata () {smsMoreList = new ArrayList <Map <String, String> (); Map <String, String> map; map = new HashMap <String, String> (); map. put ("sms_more", "Clear information"); smsMoreList. add (map); map = new HashMap <String, String> (); map. put ("sms_more", "backup"); smsMoreList. add (map); map = new HashMap <String, String> (); map. put ("sms_more", "import"); smsMoreList. add (map); map = new HashMap <String, String> (); map. put ("sms_more", "recharge payment"); smsMoreList. add (map); map = new HashMa P <String, String> (); map. put ("sms_more", "set"); smsMoreList. add (map); map = new HashMap <String, String> (); map. put ("sms_more", "log out"); smsMoreList. add (map);} private void initPopup () {smorepopulayout = View. inflate (this, R. layout. popupwindow_layout_right, null); ListView mMorePopupList = (ListView) smsMorePopuLayout. findViewById (R. id. lv_popup_list); mSmsMorePopup = new PopupWindow (smsMorePopuLayout); mSm SMorePopup. setWidth (LayoutParams. WRAP_CONTENT); mSmsMorePopup. setHeight (LayoutParams. WRAP_CONTENT); mMorePopupList. setAdapter (new SimpleAdapter (MainActivity. this, smsMoreList, R. layout. list_item_popupwindow, new String [] {"sms_more"}, new int [] {R. id. TV _list_item}); mMorePopupList. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {switch (position) {case 0: // mDialController. dialMenu (DialController. CLEAR); Toast. makeText (MainActivity. this, "EMPTY record displayed in Toast test", 0 ). show (); morePopuExit (); break; case 1: Toast. makeText (MainActivity. this, "Toast test pop-up backup", 0 ). show (); morePopuExit (); break; case 2: Toast. makeText (MainActivity. this, "import in the Toast test pop-up", 0 ). show (); morePopuExit (); break; case 3: Toast. makeText (MainActivity. this, "top-up payment in Toast test", 0 ). show (); morePopuExit (); break; case 4: Toast. makeText (MainActivity. this, "Toast test pop-up Settings", 0 ). show (); morePopuExit (); break; case 5: morePopuExit (); break; default: break;} private void morePopuExit () {ScaleAnimation animation = new ScaleAnimation (1f, 0f, 1f, 0f, Animation. RELATIVE_TO_SELF, 1.0f, Animation. RELATIVE_TO_SELF, 0.0f); animation. setDuration (250); animation. setFillAfter (true); smsMorePopuLayout. startAnimation (animation); popupExitAlpha (); animation. listener (new AnimationListener () {@ Overridepublic void onAnimationStart (Animation animation) {}@ Overridepublic void Merge (Animation animation) {complete. dismiss (); // close}) ;}}); mSmsMorePopup. setFocusable (true); mSmsMorePopup. setBackgroundDrawable (new BitmapDrawable (); mSmsMorePopup. setAnimationStyle (R. style. animation); mSmsMorePopup. setOutsideTouchable (true);} private void popupShowAlpha () {WindowManager. layoutParams params = getWindow (). getAttributes (); params. alpha = 0.6f; getWindow (). setAttributes (params);} private void popupExitAlpha () {WindowManager. layoutParams params = getWindow (). getAttributes (); params. alpha = 1.0f; getWindow (). setAttributes (params);} public int Dp2Px (Context context, float dp) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dp * scale + 0.5f);} private void onMesureLocation (View view, PopupWindow popup) {Rect frame = new Rect (); getWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame); int mMorePopupMarginTop = frame. top + Dp2Px (MainActivity. this, 12); int mMorePopupMarginRight = Dp2Px (MainActivity. this, 16); popupShowAlpha (); popup. showAtLocation (findViewById (R. id. rl_root), Gravity. RIGHT | Gravity. TOP, mMorePopupMarginRight, mMorePopupMarginTop); ScaleAnimation animation = new ScaleAnimation (0f, 1f, 0f, 1f, Animation. RELATIVE_TO_SELF, 1.0f, Animation. RELATIVE_TO_SELF, 0.0f); animation. setDuration (250); animation. setFillAfter (true); view. startAnimation (animation);} @ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. iv_common_more: if (mSmsMorePopup. isShowing () {mSmsMorePopup. dismiss (); // close} onMesureLocation (smsMorePopuLayout, mSmsMorePopup); break; default: break ;}}}
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" > <RelativeLayout 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_marginRight="15dp" android:src="@drawable/top_more_n" android:visibility="visible" /> </RelativeLayout> </RelativeLayout>
List_item_popup1_1_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:gravity="left" android:orientation="vertical" > <TextView android:id="@+id/tv_list_item" android:layout_width="fill_parent" android:layout_height="35dp" android:textSize="16dp" android:textColor="#111111" android:text="aaa" android:gravity="center" android:background="@drawable/popupwindow_list_item_text_selector" /></LinearLayout>
Popupwindow_layout_right.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:focusableInTouchMode="true" android:divider="@null" android:listSelector="@drawable/popupwindow_list_item_text_selector" android:paddingBottom="5dp" android:paddingTop="5dp" android:scrollbars="none" /></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" indicates that the x axis is relative to the parent control, starting from x = 0 of the parent control. fromYDelta = "0% p" indicates that the Y axis is relative to the parent control. y = 0 out of toXDelta = "100% p" indicates that the x axis is relative to the x = 100 position of the parent control to reach the parent control, that is, the x axis is at the end of the screen toYDelta = "100% p "the y axis is relative to the parent control's position of y = 100, that is, the X axis is at the end of the screen --> <scale android: duration = "250" android: Platform Tx = "100%" android: Platform ty = "0%" android: fromXScale = "1.0" android: toXScale = "0.0" android: fromYScale = "1.0" android: toYScale = "0.0"/> </set>
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>
<style name="animation"> <item name="android:windowExitAnimation">@anim/out</item> </style>