美文网首页
BasePopupWindow(上下左右方向弹出)

BasePopupWindow(上下左右方向弹出)

作者: 楷桐 | 来源:发表于2017-07-15 16:32 被阅读252次

    第一步,为了方便新建Popuwindow,自建BasePopupWindow,里面封装了PopupWindow 基于 view 位置显示的方法

    BasePopupWindow.java
    /***
     * PopupWindow基类
     * 
     * @author pei
     * @version 1.0
     * @cretae 2016-7-21
     * @注:若要popwindow点击外部消失,则设置 this.setFocusable(true)
     *     若要popwindow点击外部不消失,不做setFocusable设置,也不要设置成this.setFocusable(false)
     * 
     */
    public abstract class BasePopupWindow extends PopupWindow {
    
        protected View mLayoutView;
        protected int mLayoutId;
        protected Context mContext;
        protected int mWidth;
        protected int mHeight;
    
        public BasePopupWindow(int width, int height, int layoutId, Context context) {
            this.mWidth = width;
            this.mHeight = height;
            this.mLayoutId = layoutId;
            this.mContext = context;
            mLayoutView = LayoutInflater.from(context).inflate(mLayoutId, null);
            setWindow();
        }
    
        /** PopupWindow基本设置 **/
        protected void setWindow() {
            this.setContentView(mLayoutView);
            this.setWidth(mWidth);
            this.setHeight(mHeight);
            // this.setFocusable(true);// 可点击
            // 实例化一个ColorDrawable颜色为半透明(半透明遮罩颜色代码#66000000)
            ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
            this.setBackgroundDrawable(dw);
        }
    
        /** PopupWindow背景设置 **/
        protected void setBackground(int color) {
            // 实例化一个ColorDrawable颜色为半透明
            ColorDrawable dw = new ColorDrawable(color);
            this.setBackgroundDrawable(dw);
        }
    
        protected abstract void initView();
        protected abstract void initData();
        protected abstract void setListener();
    
        /** PopupWindow点击间隙处理,根据实际情况重写 **/
        protected void onTouchdimiss() {
            // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
            mLayoutView.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
    //                int height = mLayoutView.getTop();
    //                int y = (int) event.getY();
    //                if (event.getAction() == MotionEvent.ACTION_UP) {
    //                    if (y < height) {
    //                        dismiss();
    //                    }
    //                }
                    return false;
                }
            });
        }
    
        /**
         * 显示在控件正上方
         * 
         * @param view
         *            依赖的控件
         * @param marginDp
         *            设置的间距(直接写数字即可,已经做过dp2px转换)
         */
        public void showAtLocationTop(View view, float marginDp) {
            mLayoutView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int popupWidth = mLayoutView.getMeasuredWidth();
            int popupHeight = mLayoutView.getMeasuredHeight();
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            showAtLocation(view, Gravity.NO_GRAVITY, (location[0] + view.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight - dp2px(marginDp));
            update();
        }
    
        /**
         * 显示在控件正下方
         * 
         * @param view
         *            依赖的控件
         * @param marginDp
         *            设置的间距(直接写数字即可,已经做过dp2px转换)
         */
        public void showAtLocationGravityBottom(View view, float marginDp) {
            mLayoutView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int popupWidth = mLayoutView.getMeasuredWidth();
            int popupHeight = mLayoutView.getMeasuredHeight();
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            showAtLocation(view, Gravity.NO_GRAVITY, (location[0]+view.getWidth()/2)-popupWidth/2,
                    location[1]+view.getHeight()+dp2px(marginDp));
            update();
        }
    
        /**显示在控件下方
         *
         * @param view 依赖的控件
         * @param marginDp  设置的间距(直接写数字即可,已经做过dp2px转换)
         */
        public void showAtLocationBottom(View view, float marginDp){
            showAsDropDown(view, 0, dp2px(marginDp));
            update();
        }
    
    
        /**
         * 显示在控件左方
         * 
         * @param view
         *            依赖的控件
         * @param marginDp
         *            设置的间距(直接写数字即可,已经做过dp2px转换)
         */
        public void showAtLocationLeft(View view, float marginDp) {
            mLayoutView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int popupWidth = mLayoutView.getMeasuredWidth();
            int popupHeight = mLayoutView.getMeasuredHeight();
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            showAtLocation(view, Gravity.NO_GRAVITY, location[0] - popupWidth - dp2px(marginDp), (location[1] + view.getHeight() / 2) - popupHeight / 2);
            update();
        }
    
        /**
         * 显示在控件右方
         * 
         * @param view
         *            依赖的控件
         * @param marginDp
         *            设置的间距(直接写数字即可,已经做过dp2px转换)
         */
        public void showAtLocationRight(View view, float marginDp) {
            mLayoutView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int popupWidth = mLayoutView.getMeasuredWidth();
            int popupHeight = mLayoutView.getMeasuredHeight();
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            showAtLocation(view, Gravity.NO_GRAVITY, location[0] + view.getWidth() + dp2px(marginDp), (location[1] + view.getHeight() / 2) - popupHeight / 2);
            update();
        }
    
        /** dp转px **/
        private int dp2px(float dpVal) {
            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, mContext.getResources().getDisplayMetrics());
        }
    
        /** 通过id获得view **/
        @SuppressWarnings("unchecked")
        protected <T extends View> T getView(int viewId) {
            View view = null;
            if (mLayoutView == null) {
                mLayoutView = LayoutInflater.from(mContext).inflate(mLayoutId, null);
            }
            view = mLayoutView.findViewById(viewId);
            return (T) view;
        }
    }
    
    

    第二步,写一个简单的TestPop继承BasePopuWindow,用于做测试,TestPop 代码如下:

    /**
     * Instruction:用于测试的popuwindow
     * <p>
     * Author:pei
     * Date: 2017/6/28
     * Description:
     */
    
    
    public class TestPop extends BasePopupWindow{
    
        public TestPop(Context context) {
            super(ScreenUtil.dp2px(100,context), ScreenUtil.dp2px(50,context), R.layout.pop_order, context);
    
            initView();
            initData();
            setListener();
        }
    
        @Override
        protected void initView() {
    
        }
    
        @Override
        protected void initData() {
            setFocusable(true);
            setAnimationStyle(R.style.popuwindow_up_style);//popuwindow显示隐藏的动画
    
        }
    
        @Override
        protected void setListener(){
    
        }
    }
    

    http://www.jianshu.com/p/c3c30357d617

    相关文章

      网友评论

          本文标题:BasePopupWindow(上下左右方向弹出)

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