对PopupWindow的封装

最近在写PopupWindow,一直在重复搬砖,而且代码繁复,实在做无用功,所以将PopupWindow进行封装。

代码采用Builder模式设计,只注重构建的属性,而不注重构建的过程。

代码已传至github,欢迎star--https://github.com/shuaijia/JsPopupWindow



贴出代码如下


/**
 * Describtion: 对popupWindow的封装
 * Created by jia on 2017/7/10.
 * 人之所以能,是相信能
 */
public class JsPopupWindow {
    private PopupWindow mPopupWindow;

    private View mContentView;

    private Context mContext;

    public JsPopupWindow(Builder builder) {
        mContext = builder.getContext();
        // 创建view
        mContentView = LayoutInflater.from(mContext).inflate(builder.getContentViewId(), null);
        // 创建popupWindow
        mPopupWindow = new PopupWindow(mContentView, builder.getWidth(), builder.getHeight(), builder.isFouse());

        mPopupWindow.setOutsideTouchable(builder.isOutSideCancle());

        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        mPopupWindow.setAnimationStyle(builder.getAnimation());

    }

    /**
     * 消失
     */
    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    /**
     * 获取控件
     *
     * @param itemId
     * @return
     */
    public View getItemView(@NonNull int itemId) {
        if (mPopupWindow != null && mContentView != null) {
            return mContentView.findViewById(itemId);
        }
        return null;
    }

    /**
     * 在父布局特定位置显示
     *
     * @param rootViewId
     * @param gravity
     * @param x
     * @param y
     * @return
     */
    public JsPopupWindow showAtLocation(int rootViewId, int gravity, int x, int y) {
        if (mPopupWindow != null) {
            View rootView = LayoutInflater.from(mContext).inflate(rootViewId, null);
            mPopupWindow.showAtLocation(rootView, gravity, x, y);
        }
        return this;
    }

    public JsPopupWindow showAsLocation(int targetViewId, int gravity, int offx, int offy) {
        if (mPopupWindow != null) {
            View targetview = LayoutInflater.from(mContext).inflate(targetViewId, null);
            mPopupWindow.showAsDropDown(targetview, offx, offy, gravity);
        }
        return this;
    }

    public JsPopupWindow showAsLocation(View targetView, int gravity, int offx, int offy) {
        if (mPopupWindow != null) {
            mPopupWindow.showAsDropDown(targetView, offx, offy, gravity);
        }
        return this;
    }

    /**
     * 根据id设置焦点监听
     *
     * @param viewid
     * @param listener
     */
    public void setOnFocusListener(int viewid, View.OnFocusChangeListener listener) {
        View view = getItemView(viewid);
        view.setOnFocusChangeListener(listener);
    }

    /**
     * 在父布局特定位置显示
     *
     * @param gravity
     * @param x
     * @param y
     * @return
     */
    public JsPopupWindow showAtLocation(View rootView, int gravity, int x, int y) {
        if (mPopupWindow != null) {
            mPopupWindow.showAtLocation(rootView, gravity, x, y);
        }
        return this;
    }


    public static class Builder {

        private Context context;

        private int contentViewId;

        private int width;

        private int height;

        private boolean fouse;

        private boolean outSideCancle;

        private int animation;

        public Context getContext() {
            return context;
        }

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

        public int getContentViewId() {
            return contentViewId;
        }

        public Builder setContentViewId(int contentViewId) {
            this.contentViewId = contentViewId;
            return this;
        }

        public int getWidth() {
            return width;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public int getHeight() {
            return height;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        public boolean isFouse() {
            return fouse;
        }

        public Builder setFouse(boolean fouse) {
            this.fouse = fouse;
            return this;
        }

        public boolean isOutSideCancle() {
            return outSideCancle;
        }

        public Builder setOutSideCancle(boolean outSideCancle) {
            this.outSideCancle = outSideCancle;
            return this;
        }

        public int getAnimation() {
            return animation;
        }

        public Builder setAnimation(int animation) {
            this.animation = animation;
            return this;
        }

        /**
         * 构建
         *
         * @return
         */
        public JsPopupWindow build() {
            return new JsPopupWindow(this);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值