美文网首页
一个popular的小实践

一个popular的小实践

作者: 基本密码宋 | 来源:发表于2017-07-04 17:59 被阅读20次
    主要思路出自:http://www.jianshu.com/p/799dbb86f908
    不过就是在人家的基础上稍作修改
    popularwidow.gif
    • PopularController popularwindow的控制器 用于初始化popular的一些属性,操作其API
      其中有内部类PopupParams 用于集中操作popular的方法。
    public class PopularController {
        private PopupWindow popuWindow;
        private Context context;
        public View contentView;
        private Window mWindow;
    
    
        public PopularController(Context context, PopupWindow popupWindow) {
            this.context = context;
            this.popuWindow = popupWindow;
        }
    
        public void createView(int layoutId) {
            contentView = null;
            this.contentView = LinearLayout.inflate(context, layoutId, null);
            initContentView();
        }
    
        public void createView(View view) {
            contentView = null;
            this.contentView = view;
            initContentView();
        }
    
        /**
         * 初始化 popularWindow中的内容 view
         */
        public void initContentView() {
            this.popuWindow.setContentView(contentView);
        }
    
        /**
         * 设置宽度和高度
         *
         * @param width
         * @param height
         */
        public void setWidthAndHeight(int width, int height) {
            if (width == 0 || height == 0) {
                this.popuWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                this.popuWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            } else {
                this.popuWindow.setWidth(width);
                this.popuWindow.setHeight(height);
            }
        }
    
        /**
         * 设置背景透明度
         *
         * @param level (0~~1)
         */
        public void setBackGroundLevel(float level) {
            mWindow = ((Activity) context).getWindow();
            WindowManager.LayoutParams params = mWindow.getAttributes();
            params.alpha = level;
            mWindow.setAttributes(params);
        }
    
        /**
         * 设置popular的动画状态
         *
         * @param animationStyle
         */
        public void setAnimationStyle(int animationStyle) {
            this.popuWindow.setAnimationStyle(animationStyle);
        }
    
    
        /**
         * 设置 popular外部是否可以点击
         *
         * @param touchable
         */
        public void setOutSideTouchable(boolean touchable) {
            this.popuWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));//设置透明度
            this.popuWindow.setOutsideTouchable(touchable);
            this.popuWindow.setFocusable(touchable);
        }
    
    
        public static class PopupParams {
            public int layoutResId;//布局id
            public Context mContext;
            public int mWidth, mHeight;//弹窗的宽和高
            public boolean isShowBg, isShowAnim;
            public float bg_level;//屏幕背景灰色程度
            public int animationStyle;//动画Id
            public View mView;
            public boolean isTouchable = true;
    
            public PopupParams(Context context) {
                this.mContext = context;
            }
    
            public void apply(PopularController controller) {
                if (mView != null) {
                    controller.createView(mView);
                } else if (layoutResId != 0) {
                    controller.createView(layoutResId);
                } else {
                    throw new IllegalArgumentException("PopupView's contentView is null");
                }
                controller.setWidthAndHeight(mWidth, mHeight);
                controller.setOutSideTouchable(isTouchable);
                if (isShowBg) {
                    controller.setBackGroundLevel(bg_level);
                }
                if (isShowAnim) {
                    controller.setAnimationStyle(animationStyle);
                }
            }
    
        }
    }
    
    • MyCommonPopularWindow popularWindow的公共类,实现了popularWindow 其中用建造者模式进行创建
    
    public class MyCommonPopularWindow extends PopupWindow {
    
        private PopularController popularController;
    
        public MyCommonPopularWindow(Context context) {
            popularController = new PopularController(context, this);
        }
    
    
        @Override
        public int getWidth() {
            return popularController.contentView.getMeasuredWidth();
        }
    
        @Override
        public int getHeight() {
            return popularController.contentView.getMeasuredHeight();
        }
    
        @Override
        public void dismiss() {
            super.dismiss();
            //小时的时候
            popularController.setBackGroundLevel(1.0f);
        }
    
        public interface ViewInterface {
            void getChildView(View view, int layoutResId);
        }
    
    
        public static class Builder {
    
            private PopularController.PopupParams params;
            private Context context;
            private ViewInterface listener;
    
            public Builder(Context context) {
                params = new PopularController.PopupParams(context);
            }
    
            public Builder setView(int layoutResId) {
                params.mView = null;
                params.layoutResId = layoutResId;
                return this;
            }
    
            public Builder setView(View view) {
                params.layoutResId = 0;
                params.mView = view;
                return this;
            }
    
            public Builder setWidthAndHeight(int width, int height) {
                params.mWidth = width;
                params.mHeight = height;
                return this;
            }
    
            public Builder setBackGroundLevel(float level) {
                params.isShowBg = true;
                params.bg_level = level;
                return this;
            }
    
            public Builder setOutsideTouchable(boolean toubalbe) {
                params.isTouchable = toubalbe;
                return this;
            }
    
            public Builder setAnimationStyle(int animationStyle) {
                params.isShowAnim = true;
                params.animationStyle = animationStyle;
                return this;
            }
    
            public Builder setViewOnclickListener(ViewInterface listener) {
                this.listener = listener;
                return this;
            }
    
    
            public MyCommonPopularWindow create() {
                MyCommonPopularWindow myCommonPopularWindow = new MyCommonPopularWindow(params.mContext);
                params.apply(myCommonPopularWindow.popularController);
                if (listener != null && params.layoutResId != 0) {
                    listener.getChildView(myCommonPopularWindow.popularController.contentView, params.layoutResId);
                }
                CommonUtil.measureWidthAndHeight(myCommonPopularWindow.popularController.contentView);
                return myCommonPopularWindow;
            }
        }
    }
    
    • 测量的工具类
    public class CommonUtil {
        /**
         * 测量View的宽高
         *
         * @param view View
         */
        public static void measureWidthAndHeight(View view) {
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(w, h);
        }
    }
    
    
    • 使用
    public class MainActivity extends AppCompatActivity implements MyCommonPopularWindow.ViewInterface {
    
        private Button btn;
        private MyCommonPopularWindow myCommonPopularWindow;
        private Button btn_right;
        private Button btn_left;
        private Button btn_top;
        private Button btn_all;
        private Button btn_custom;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn = (Button) findViewById(R.id.btn);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myCommonPopularWindow != null && myCommonPopularWindow.isShowing())
                        return;
                    myCommonPopularWindow = new MyCommonPopularWindow.Builder(MainActivity.this).setView(R.layout.layout_popular).setBackGroundLevel(0.5f)
                            .setOutsideTouchable(true)
                            .setWidthAndHeight(0, 0)
                            .setViewOnclickListener(MainActivity.this).create();
    
                    //向下弹框
    
    //                myCommonPopularWindow.showAsDropDown(v);
    
    //          findViewById(android.R.id.content) 整个 viewGroup的跟布局
                    int[] positions = new int[2];
                    v.getLocationOnScreen(positions);
                    myCommonPopularWindow.showAtLocation(findViewById(android.R.id.content), Gravity.NO_GRAVITY, 0, positions[1] + v.getHeight());
                }
            });
    
            //向右弹框
            btn_right = (Button) findViewById(R.id.btn_right);
            btn_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myCommonPopularWindow != null && myCommonPopularWindow.isShowing())
                        return;
                    myCommonPopularWindow = new MyCommonPopularWindow.Builder(MainActivity.this).setView(R.layout.layout_popular).setBackGroundLevel(0.5f)
                            .setOutsideTouchable(true)
                            .setWidthAndHeight(0, 0)
                            .setViewOnclickListener(MainActivity.this).create();
    //                myCommonPopularWindow.showAsDropDown(v, v.getWidth(), -v.getHeight());
    
                    //得到button的左上角坐标
                    int[] positions = new int[2];
                    v.getLocationOnScreen(positions);
                    myCommonPopularWindow.showAtLocation(findViewById(android.R.id.content), Gravity.NO_GRAVITY, positions[0] + v.getWidth(), positions[1]);
    
                }
    
            });
    
            //向左边弹框
            btn_left = (Button) findViewById(R.id.btn_left);
            btn_left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myCommonPopularWindow != null && myCommonPopularWindow.isShowing())
                        return;
                    myCommonPopularWindow = new MyCommonPopularWindow.Builder(MainActivity.this).setView(R.layout.layout_popular).setBackGroundLevel(0.5f)
                            .setOutsideTouchable(true)
                            .setWidthAndHeight(0, 0)
                            .setViewOnclickListener(MainActivity.this).create();
    //                myCommonPopularWindow.showAsDropDown(v, -myCommonPopularWindow.getWidth(), -v.getHeight());
    
                    //得到button的左上角坐标
                    int[] positions = new int[2];
                    v.getLocationOnScreen(positions);
                    myCommonPopularWindow.showAtLocation(findViewById(android.R.id.content), Gravity.NO_GRAVITY, positions[0] - myCommonPopularWindow.getWidth(), positions[1]);
                }
            });
    
    
            //向上弹框
            btn_top = (Button) findViewById(R.id.btn_top);
            btn_top.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myCommonPopularWindow = new MyCommonPopularWindow.Builder(MainActivity.this).setView(R.layout.layout_popular_2).setBackGroundLevel(0.5f)
                            .setOutsideTouchable(true)
                            .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                            .setViewOnclickListener(MainActivity.this).create();
    //               myCommonPopularWindow.showAsDropDown(v, 0, -(myCommonPopularWindow.getHeight() + v.getMeasuredHeight()));
    
                    //得到button的左上角坐标
                    int[] positions = new int[2];
                    v.getLocationOnScreen(positions);
                    myCommonPopularWindow.showAtLocation(findViewById(android.R.id.content), Gravity.NO_GRAVITY, positions[0], positions[1] - myCommonPopularWindow.getHeight());
                }
            });
    
            //全屏的弹框
            btn_all = (Button) findViewById(R.id.btn_all);
            btn_all.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myCommonPopularWindow = new MyCommonPopularWindow.Builder(MainActivity.this).setView(R.layout.layout_popular_2).setBackGroundLevel(0.5f)
                            .setOutsideTouchable(true)
                            .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                            .setViewOnclickListener(MainActivity.this).create();
                    myCommonPopularWindow.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
                }
            });
    
            //相对的具体于某个控件的弹框
            btn_custom = (Button) findViewById(R.id.btn_custom);
            btn_custom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myCommonPopularWindow = new MyCommonPopularWindow.Builder(MainActivity.this).setView(R.layout.layout_info).setBackGroundLevel(0.5f)
                            .setOutsideTouchable(true)
                            .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                            .setViewOnclickListener(MainActivity.this).create();
                    myCommonPopularWindow.showAsDropDown(v, (int) (-myCommonPopularWindow.getWidth() + DpUtil.dp2px(MainActivity.this, 20)), -(myCommonPopularWindow.getHeight() + v.getMeasuredHeight()));
    
                }
            });
    
    
        }
    
    
        @Override
        public void getChildView(View view, int layoutResId) {
            switch (layoutResId) {
                case R.layout.layout_popular:
                    TextView tv2 = (TextView) view.findViewById(R.id.tv2);
                    tv2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(MainActivity.this, "TV2", Toast.LENGTH_SHORT).show();
                            if (myCommonPopularWindow != null) {
                                myCommonPopularWindow.dismiss();
                            }
                        }
                    });
                    break;
    
                default:
                    break;
            }
        }
    }
    

    https://github.com/jenosongjiabin/PopularDemo

    相关文章

      网友评论

          本文标题:一个popular的小实践

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