Custom PopupWindow + ListView + Anim, custom popupwindow

Source: Internet
Author: User

Custom PopupWindow + ListView + Anim, custom popupwindow


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>


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 comment; @ 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 (msmorepopup. 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, "The Empty record is displayed in the Toast test", Toast. LENGTH_SHORT ). show (); mSmsMorePopup. thisDismiss (AnimStyle. RIGHTANIM); break; case 1: Toast. makeText (MainActivity. this, "Toast test pop-up backup", Toast. LENGTH_SHORT ). show (); mSmsMorePopup. thisDismiss (AnimStyle. RIGHTANIM); break; case 2: Toast. makeText (MainActivity. this, "import in the Toast test pop-up", Toast. LENGTH_SHORT ). show (); mSmsMorePopup. thisDismiss (AnimStyle. RIGHTANIM); break; case 3: Toast. makeText (MainActivity. this, "top-up payment in Toast test", 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; im Port 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. onDi SmissListener; import android. widget. textView; import android. widget. linearLayout. layoutParams; public class CommonPopuWindow extends PopupWindow implements AnimationListener, listener {private Activity mActivity; private View rootView; private ListView mListView; private Resources mResources; private DataAdapter mAdapter; public enum AnimStyle {LEFTANIM, RIGHTANIM} private ScaleAnimation leftShowA Nim, 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 ("de Precation ") 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. setBackgroundDraw Able (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);}/*** display */public void show (View anchor, int xoff, int yoff, AnimStyle style) {this. showAsDropDown (anchor, xoff, yoff); popupShowAlpha (); showAnim (style);} public void show (AnimSty Le style) {Rect frame = new Rect (); mActivity. getWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame); int mMorePopupMarginTop = frame. top + dp2Px (mActivity, 12); int mMorePopupMarginRight = dp2Px (mActivity, 16); popupShowAlpha (); this. showAtLocation (mActivity. findViewById (R. id. rl_root), Gravity. RIGHT | Gravity. TOP, mMorePopupMarginRight, mMorePopupMarginTop); showAnim (style);}/*** display animation effect */priva Te void showAnim (AnimStyle style) {switch (style) {case LEFTANIM: if (leftShowAnim = null) {leftShowAnim = new ScaleAnimation (0f, 1f, 0f, 1f, Animation. RELATIVE_TO_SELF, 0.0f, Animation. RELATIVE_TO_SELF, 0.0f); leftShowAnim. setduration( 250); 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 (250); rightShowAnim. setFillAfter (true);} rootView. startAnimation (rightShowAnim); break ;}/ *** exit animation effect */public void thisDismiss (AnimStyle style) {switch (style) {case LEFTANIM: if (leftExitAnim = null) {leftExitAnim = new ScaleAnimation (1f, 0f, 1f, 0f, Animation. RELATIVE_TO_SELF, 0.0f, Animation. RELATIV E_TO_SELF, 0.0f); leftExitAnim. setDuration (1, 250); 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); rightExitAni M. 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 (); params. alpha = 0.6f; window. setAttributes (params);} private void popupExitAlpha () {Window window Window = (Activity) MActivity ). getWindow (); WindowManager. layoutParams params = window. getAttributes (); params. 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_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:orientation="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" 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>

In addition, 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>


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.