欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

问题记录-PopupWindow的setOnDismissListener失效问题

程序员文章站 2022-03-03 18:52:55
...

公司项目里有一个列表弹窗的需求,就自定义了个PoppupWindow,弹出从底部平移动画弹出,并且弹窗高度固定(因为可能有很多条数据,如果MatchParent会很丑)。在弹窗show的时候,我给整个window设置了个0.5的透明度,然后弹窗消失的时候再设置回来,本来想着在setOnDismissListener的回调方法里添加设置回来的操作,但回调不起作用。最后想了想,直接重写了PopupWindow的dismiss()方法,在该方法里添加设置回来的操作,即可,下面是大致的代码逻辑:

public class TestPopupWindow extends PopupWindow {

    private LinearLayout mLlTitle;
    private TextView mTvTitle;
    private ListView mLvData;
    private QualityPopupAdapter mAdapter;
    private OnClickItemListener listener;
    private Context mContext;

    public void setListener(OnClickItemListener listener) {
        this.listener = listener;
    }

    public TestPopupWindow(final Context context) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.qm_popupwindow_list, null);

        setContentView(view);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        update();
        setBackgroundDrawable(new ColorDrawable(0000000000));
        //设置弹入弹出动画
        setAnimationStyle(R.style.qm_style_popup_window);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        setBackgroundAlpha(1.0f);
    }

    /**
     * 显示
     *
     * @param view
     */
    public void show(View view) {
        if (isShowing()) {
            dismiss();
        } else {
            setBackgroundAlpha(0.5f);
            showAtLocation(view, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 改变背景透明度
     *
     * @param alpha
     */
    private void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams layoutParams = ((Activity)      
            mContext).getWindow().getAttributes();
        layoutParams.alpha = alpha;
        ((Activity) mContext).getWindow().setAttributes(layoutParams);
    }
 }