借助PopupWindow实现的一种通用弹窗

来源:互联网 发布:电力施工软件 编辑:程序博客网 时间:2024/06/05 08:38
记录下自己实现的一个通用弹窗,使用的时候传入自己需要的view即可

public class PopWindow {    private Activity mContext;    private ViewGroup contentView;    private PopupWindow popupWindow;    public interface OnPopWindowDismissListener {        void onPopWindowDismiss();    }    private OnPopWindowDismissListener onPopWindowDismissListener;    public void setOnPopWindowDismissListener(OnPopWindowDismissListener listener) {        this.onPopWindowDismissListener = listener;    }    private void createPopWindow(View contentView, int width, int height) {        //设置弹出框的宽度和高度        popupWindow = new PopupWindow(contentView, width, height);//        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        popupWindow.setFocusable(true);// 取得焦点        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的        popupWindow.setBackgroundDrawable(new BitmapDrawable());        //点击外部消失        popupWindow.setOutsideTouchable(true);        //设置可以点击        popupWindow.setTouchable(true);        //进入退出的动画        popupWindow.setAnimationStyle(R.style.pop_window_anim_style);    }    public void showPopWindow(View contentView, final Activity context, int width, int height) {        this.mContext = context;        this.contentView = (ViewGroup) contentView;        createPopWindow(contentView, width, height);        popupWindow.showAtLocation(contentView, Gravity.CENTER, 0, 0);        final WindowManager.LayoutParams wlBackground = context.getWindow().getAttributes();        wlBackground.alpha = 0.5f;      // 0.0 完全不透明,1.0完全透明        context.getWindow().setAttributes(wlBackground);        // 当PopupWindow消失时,恢复其为原来的颜色        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {            @Override            public void onDismiss() {                wlBackground.alpha = 1.0f;                context.getWindow().setAttributes(wlBackground);                if (onPopWindowDismissListener != null) {                    onPopWindowDismissListener.onPopWindowDismiss();                }            }        });    }    public void dismiss() {        if (popupWindow != null)            popupWindow.dismiss();    }    public boolean isShowing() {        if (popupWindow != null)            return popupWindow.isShowing();        return false;    }}

传入的view(kotlin实现)

class SecKillEndView(var mcontext: Context) : FrameLayout(mcontext) {    private var clickListener: OnButtonClickListener? = null    interface OnButtonClickListener {        fun OnButtonClick()    }    fun setOnButtonClickListener(listener: OnButtonClickListener) {        clickListener = listener    }    init { initContentLayout() }    private fun initContentLayout() {        blurBgLayout(mcontext) {            layoutParams = LayoutParams(matchParent, matchParent)            setPageType(BlurBgLayout.PAGETYPE.OTHER_PAGE)        }        linearLayout {            layoutParams = {                val params = FrameLayout.LayoutParams(getDimension(R.dimen.w_1062px), getDimension(R.dimen.h_492px))                orientation = LinearLayout.VERTICAL                params            }.invoke()            // 标题            textView {                textSize = CoocaaApplication.Dpi(SkyTextSize.t_7).toFloat()                textColor = mcontext.resources.getColor(R.color.c_3)                text = mcontext.getString(R.string.mall_purchase_seckill_sold_out)            }.lparams(wrapContent, wrapContent) {                gravity = Gravity.CENTER_HORIZONTAL                topMargin = getDimension(R.dimen.h_136px)            }            //描述            textView {                text = mcontext.getString(R.string.mall_purchase_seckill_end_restore_original_price)                textSize = CoocaaApplication.Dpi(SkyTextSize.t_4).toFloat()                textColor = mcontext.resources.getColor(R.color.c_3)            }.lparams(wrapContent, wrapContent) {                gravity = Gravity.CENTER_HORIZONTAL                topMargin = getDimension(R.dimen.h_20px)            }            //按钮:我知道了            button {                text = mcontext.getString(R.string.mall_order_manager_detail_i_see)                textSize = CoocaaApplication.Dpi(SkyTextSize.t_4).toFloat()                textColor = mcontext.resources.getColor(R.color.a_5)                backgroundDrawable = mcontext.resources.getDrawable(R.drawable.ui_sdk_btn_focus_shadow_bg)                setOnClickListener({ clickListener?.OnButtonClick() })            }.lparams(getDimension(R.dimen.w_464px), getDimension(R.dimen.h_233px)) {                gravity = Gravity.CENTER_HORIZONTAL                topMargin = CoocaaApplication.Div(-5)            }        }    }}

使用者(kotlin代码):

class SecKillEndDialog(val activity: Activity) : SecKillEndView.OnButtonClickListener {    private var popWindow: PopWindow? = null    private var secKillEndView: SecKillEndView? = null    init {        if (secKillEndView == null)            secKillEndView = SecKillEndView(activity)        secKillEndView!!.setOnButtonClickListener(this)    }     fun showDialog() {        if (popWindow == null)            popWindow = PopWindow()        popWindow?.showPopWindow(secKillEndView, activity, CoocaaApplication.Div(1062), CoocaaApplication.Div(492))    }    override fun OnButtonClick() {        if (popWindow != null)            popWindow!!.dismiss()    }}