Android Dialog 实现仿iOS UIActionSh

作者: 知识酷教育大东 | 来源:发表于2016-12-16 10:39 被阅读1534次

    啊哈 补个GIF 感谢Coding_css的建议

    actionsheet.gif

    拍照、和相册设定为动态加载。。底部cancel设定为固定视图
    <h2>首先看实际调用

       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:15002102128@126.com
     */
    
    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<titleItems.length;i++){//初始化操作按钮
                        final int position=i;
                        Button btn= (Button) inflater.inflate(R.layout.ld_actionsheet_item,null);
                        btn.setText(titleItems[i]);
                        btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View V) {
                                //消失动画
                                    LD_AnimationUtil.translateDown(context, view, new LD_AnimationUtil.LD_AnimationListener() {
                                        @Override
                                        public void ld_AnimationFinish() {
                                            if (listener!=null)
                                                listener.itemSelect(actionSheet,position+1);//动画完成,监听点击
                                        }
                                    });
       
    
                            }
                        });
                        LinearLayout.LayoutParams LayoutParams =new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        LayoutParams.setMargins(0,8,0,0);
                        btn.setLayoutParams(LayoutParams);
                        llItem.addView(btn);
                    }
                }
                Button btnCancel= (Button) view.findViewById(R.id.actioncancel);
                if (cancelTitle!=null){
                    btnCancel.setText(cancelTitle);//设置取消键文字  没放在for循环动态添加
                }
    
                btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
                        LD_AnimationUtil.translateDown(context, view, new LD_AnimationUtil.LD_AnimationListener() {
                            @Override
                            public void ld_AnimationFinish() {
                                if (listener!=null)
                                    listener.itemSelect(actionSheet,0);
                            }
                        });
                    }
                });
    
                actionSheet.getWindow().setGravity(Gravity.BOTTOM);
                Window window = actionSheet.getWindow();
                window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
                window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
    
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(lp);
                actionSheet.addContentView(view, new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));//添加ContentView
                actionSheet.setCancelable(false);
    
                actionSheet.setmContentView(view);//Dialog 保存ContentView 进入动画要用到 好像没有直接获取的方法??
                return  actionSheet;
            }
            public interface OnActionSheetselectListener {
                void  itemSelect(Dialog dialog,int index);
            }
        }
        //Dialog显示时执行动画
        @Override
        public void show() {
            super.show();
            LD_AnimationUtil.translateUp(getContext(),mContentView);
        }
    
        @Override
        public void dismiss() {
            super.dismiss();
    
        }
        //创建Dialog 时 保存ContentView
        public void setmContentView(View mContentView) {
            this.mContentView = mContentView;
        }
    }
    

    布局文件 ld_actionsheet.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/ll_items"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/commonMiddlePadding"
            android:orientation="vertical">
    
        </LinearLayout>
        <Button
            android:id="@+id/actioncancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="取消"
            android:padding="@dimen/commonPadding"
            android:background="@drawable/btn_white_bg"
            android:textSize="@dimen/textTileSize"
            android:textColor="@color/black"
            android:layout_marginRight="@dimen/commonMiddlePadding"
            android:layout_marginTop="@dimen/commonPadding"
            android:layout_marginBottom="@dimen/commonMiddlePadding"
            android:layout_marginLeft="@dimen/commonMiddlePadding"/>
    </LinearLayout>
    

    Dialog样式

     <style name="Dialog" parent="android:style/Theme.Dialog">
            <item name="android:background">#00000000</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowIsFloating">true</item>
        </style>
    

    用到的动画

    /**
         * 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

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
           <corners android:radius="@dimen/commonSmallPadding" >
        </corners>
    
        <solid android:color="@color/white" />
    
    </shape>
    

    btn_white_pressed_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <corners android:radius="@dimen/commonSmallPadding" >
        </corners>
    
        <solid android:color="@color/gray" />
    
    </shape>
    

    btn_white_bg.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/btn_white_pressed_bg"></item>
        <item android:state_pressed="false" android:drawable="@drawable/btn_white_normal_bg"></item>
    
    </selector>
    

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

    相关文章

      网友评论

        本文标题:Android Dialog 实现仿iOS UIActionSh

        本文链接:https://www.haomeiwen.com/subject/bflnmttx.html