Android Dialog 实现仿iOS UIActionSheet

啊哈 补个GIF 感谢Coding_css的建议

Android Dialog 实现仿iOS UIActionSheet_第1张图片
actionsheet.gif

拍照、和相册设定为动态加载。。底部cancel设定为固定视图

首先看实际调用

   String[] arr={"拍照","相册"};
                    LD_DialogUtil.showActionSheet(this, arr, "取消", new LD_ActionSheet.Builder.OnActionSheetselectListener() {
                        @Override
                        public void itemSelect(Dialog dialog,int index) {
                            dialog.dismiss();
                        switch (index){
                            case 0://底部按钮
                              showToast("取消");
                                break;
                            case 1:
                                showToast("拍照");

                                break;
                            case 2:
                                showToast("相册");
                                break;
                        }
                        }
                    });

然后是调用方法,创建内部类Builder 然后构造LD_ActionSheet

    /**
     * UIActionSheet
     * @param context 上下文
     * @param items   要加载的按钮文字数组
     * @param cancel   底部按钮问题
     * @param listener  按钮点击监听
     */
    public static void  showActionSheet(Context context, String[] items, String cancel, LD_ActionSheet.Builder.OnActionSheetselectListener listener){
        LD_ActionSheet.Builder builder=new LD_ActionSheet.Builder(context);
        builder.setcancelString(cancel).setItems(items).setListner(listener);
        builder.create().show();
    }

再看自定义LD_ActionSheet.java 类


/**
 * Created by LiuDong on 2016/12/15.
 * Email:[email protected]
 */

public class LD_ActionSheet extends Dialog {
    private View mContentView;
    public LD_ActionSheet(Context context) {
        super(context);

    }

    public LD_ActionSheet(Context context, int theme) {
        super(context, theme);

    }

    protected LD_ActionSheet(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);

    }
    public  static class Builder{
        private Context context;
        private String[]  titleItems;//按钮问题数组
        private String  cancelTitle;//取消按钮文字
        private OnActionSheetselectListener listener;//按钮点击监听

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置显示数组
         * @param items
         * @return
         */

        public Builder setItems(String[] items){
            this.titleItems=items;
            return this;
        }

        /**
         * 设置取消按钮
         * @param cancel
         * @return
         */
        public  Builder setcancelString(String cancel){
            this.cancelTitle=cancel;
            return this;
        }

        /**
         * 设置按钮点击监听
         * @param listner
         * @return
         */
        public Builder setListner(OnActionSheetselectListener listner){
            this.listener=listner;
            return this;
        }

        /**
         * 创建ActionSheet
         * @return
         */
        public LD_ActionSheet create(){
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View view =inflater.inflate(R.layout.ld_actionsheet,null);//布局解析
            LinearLayout llItem= (LinearLayout) view.findViewById(R.id.ll_items);
            final     LD_ActionSheet actionSheet= new  LD_ActionSheet(context,R.style.Dialog);//创建Dialog
            if (titleItems!=null){
                for (int i=0;i

布局文件 ld_actionsheet.xml



    

    
    

Dialog样式

 

用到的动画

/**
     * view show from the bottom up;
     * 
     * @param context
     * @param view
     */
    public static void translateUp(Context context, View view) {
    

        ObjectAnimator
                .ofFloat(view, "translationY", view.getMeasuredHeight(), 0)
                .setDuration(300).start();

    }

    /**
     * view dismiss from the top down;
     * 
     * @param context
     * @param view
     */
    public static void translateDown(Context context, View view,
            final LD_AnimationListener listener) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY",
                0, view.getMeasuredHeight()).setDuration(300);
        animator.addListener(new AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (listener != null) {
                    listener.ld_AnimationFinish();
                }

            }

            @Override
            public void onAnimationCancel(Animator animation) {
                // TODO Auto-generated method stub

            }
        });
        animator.start();

    }

按钮样式,这个感觉差点什么,我写个样式一般要三个xml文件
normal pressed shape 以及一个selector ,是不是可以整合在一起?

btn_white_normal_bg.xml



       
    

    


btn_white_pressed_bg.xml




    
    

    


btn_white_bg.xml




    
    


我擦嘞,基本上是纯贴代码了,好尴尬啊。写这篇文章是有感,初期实现是用PopupWindow,再看 感觉代码封装性不是很好,于是参考网上的一些例子demo,重新整理撰写,哎 羡慕那些有大神带的。一路自己摸爬滚打到现在,心塞塞啊。
好了不闲扯了,希望对大家有点帮助

你可能感兴趣的:(Android Dialog 实现仿iOS UIActionSheet)