package com.githang.android.actionsheet;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;


/**
 * @author haohang
 * @version 15-5-24.
 */
public class ActionSheetDialog extends Dialog {
    private Button mCancel;
    private ListView mMenuItems;
    private ArrayAdapter<String> mAdapter;

    private MenuListener mMenuListener;
    private View mRootView;

    private Animation mShowAnim;
    private Animation mDismissAnim;

    private boolean isDismissing;

    public ActionSheetDialog(Context context) {
        super(context, R.style.ActionSheetDialog);
        getWindow().setGravity(Gravity.BOTTOM);
        initView(context);
    }

    private void initView(Context context) {
        mRootView = View.inflate(context, R.layout.dialog_action_sheet, null);
        mCancel = (Button) mRootView.findViewById(R.id.menu_cancel);
        mMenuItems = (ListView) mRootView.findViewById(R.id.menu_items);
        mAdapter = new ArrayAdapter<String>(context, R.layout.menu_item) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                setBackground(position, view);
                return view;
            }

            private void setBackground(int position, View view) {
                int count = getCount();
                if (count == 1) {
                    view.setBackgroundResource(R.drawable.menu_item_single);
                } else if (position == 0) {
                    view.setBackgroundResource(R.drawable.menu_item_top);
                } else if (position == count - 1) {
                    view.setBackgroundResource(R.drawable.menu_item_bottom);
                } else {
                    view.setBackgroundResource(R.drawable.menu_item_middle);
                }
            }
        };
        mMenuItems.setAdapter(mAdapter);
        this.setContentView(mRootView);
        initAnim(context);
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
        mMenuItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mMenuListener != null) {
                    mMenuListener.onItemSelected(position, mAdapter.getItem(position));
                    dismiss();
                }
            }
        });
        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if(mMenuListener != null) {
                    mMenuListener.onCancel();
                }
            }
        });
    }

    private void initAnim(Context context) {
        mShowAnim = AnimationUtils.loadAnimation(context, R.anim.translate_up);
        mDismissAnim = AnimationUtils.loadAnimation(context, R.anim.translate_down);
        mDismissAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismissMe();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public ActionSheetDialog addMenuItem(String items) {
        mAdapter.add(items);
        return this;
    }

    public void toggle() {
        if (isShowing()) {
            dismiss();
        } else {
            show();
        }
    }

    @Override
    public void show() {
        mAdapter.notifyDataSetChanged();
        super.show();
        mRootView.startAnimation(mShowAnim);
    }

    @Override
    public void dismiss() {
        if(isDismissing) {
            return;
        }
        isDismissing = true;
        mRootView.startAnimation(mDismissAnim);
    }

    private void dismissMe() {
        super.dismiss();
        isDismissing = false;
    }

    public MenuListener getMenuListener() {
        return mMenuListener;
    }

    public void setMenuListener(MenuListener menuListener) {
        mMenuListener = menuListener;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public interface MenuListener {
        void onItemSelected(int position, String item);

        void onCancel();
    }
}

@@ -483,23 +483,73 @@
 %0A * 
-@author haohang
+ActionSheet for Android.%0A * @author haohang (msdx.android@qq.com)
 %0A * 
@@ -557,16 +557,20 @@
 version 
+0.1 
 15-5-24.
@@ -570,16 +570,30 @@
 5-5-24.%0A
+ * @since 0.1%0A
  */%0Apubl
@@ -3918,32 +3918,153 @@
      %7D);%0A    %7D%0A%0A
+    /**%0A     * Add menu item.%0A     * @param item the text of the item be added.%0A     * @return%0A     * @since 0.1%0A     */%0A
     public Actio
@@ -4099,17 +4099,16 @@
 ing item
-s
 ) %7B%0A    
@@ -4124,25 +4124,24 @@
 ter.add(item
-s
 );%0A        r
@@ -4151,32 +4151,95 @@
 rn this;%0A    %7D%0A%0A
+    /**%0A     * Show or dismiss menu.%0A     * @since 0.1%0A     */%0A
     public void 
@@ -4775,32 +4775,96 @@
 = false;%0A    %7D%0A%0A
+    /**%0A     * Return the menu listener.%0A     * @return%0A     */%0A
     public MenuL
@@ -4920,32 +4920,122 @@
 istener;%0A    %7D%0A%0A
+    /**%0A     * Set the menu listener%0A     * @param menuListener%0A     * @isnce 0.1%0A     */%0A
     public void 
@@ -5367,96 +5367,479 @@
     
-public interface MenuListener %7B%0A        void onItemSelected(int position, String item);%0A
+/**%0A     * Menu listener.%0A     * @since 0.1%0A     */%0A    public interface MenuListener %7B%0A        /**%0A         * When one of the menu items is selected, this method is called.%0A         * @param position the position of the menu item%0A         * @param item the text of the menu item%0A         * @since 0.1%0A         */%0A        void onItemSelected(int position, String item);%0A%0A        /**%0A         * This method is called when cancel the menu.%0A         * @since 0.1%0A         */
 %0A   

Add comment.