PopupWindow在7.0上显示的坑

下面是项目中封装的一个展示筛选框的popupWindow类,

public class FilterDialogView extends PopupWindow {

    // 当前选中位置
    private int mSelectIndex = 0;
    private Context mContext;
    private boolean mNeedPressed = false;
    private ListView mListView;
    private OnFilterDialogItemSelectListener mCallBack;

    public FilterDialogView(Context context) {
        super(context);
        mContext = context;
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));
        initView();
    }

    private void initView() {
        // 底部半透明View
        LinearLayout.LayoutParams rp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout relativeLayout = new RelativeLayout(mContext);
        relativeLayout.setBackgroundColor(Color.argb(150, 0, 0, 0));
        // 点击消失
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setContentView(relativeLayout);

        int w = (int) mContext.getResources().getDimension(R.dimen.x260);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(w, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

        mListView = new ListView(mContext);
        mListView.setBackgroundColor(Color.parseColor("#FFFFFF"));
//        mListView.setDivider(new ColorDrawable(0xffdddddd));
        mListView.setDivider(mContext.getResources().getDrawable(R.drawable.listview_spacing_line));
        mListView.setDividerHeight(1);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                if (mCallBack == null)
                    return;
                mCallBack.onItemSelect(position);
            }
        });

        relativeLayout.addView(mListView, lp);
    }

    /**
     * 显示
     *
     * @param anchor
     */
    public void showAsDropDown(View anchor) {
        if (!this.isShowing()) {
            this.showAsDropDown(anchor, 0, 0);
        } else {
            this.dismiss();
        }
    }

    /**
     * 设置控件选项
     *
     * @param titles 标题选项
     */
    public void setTitles(List<String> titles) {
        mListView.setAdapter(new AdapterBase<String>(mContext, titles, R.layout.adapter_filter_dialog) {
            @Override
            public void convertView(ViewHolder helper, String item) {
                helper.setText(R.id.tx_filter_dialog_title, item);

                if (!mNeedPressed) {
                    if (helper.getPosition() == mSelectIndex) {
                        // 设置选中字体颜色
                        helper.setTextColor(R.id.tx_filter_dialog_title, Color.parseColor("#52bff1"));
                    } else {
                        // 设置未选中字体颜色
                        helper.setTextColor(R.id.tx_filter_dialog_title, Color.parseColor("#aaaaaa"));
                    }
                }
            }
        });
    }

    public void setSelectIndex(int index) {
        mSelectIndex = index;
    }

    /**
     * 设置 击回调
     *
     * @param callBack 回调接口
     */
    public void setOnItemSelectListener(OnFilterDialogItemSelectListener callBack) {
        mCallBack = callBack;
    }

    public interface OnFilterDialogItemSelectListener {
        void onItemSelect(int index);
    }

    public void isNeedPressed(boolean needPressed) {
        this.mNeedPressed = needPressed;
    }

}

完美使用,没有问题
这里写图片描述

但今天突然发现,成了下面这个样子了
这里写图片描述

反复核查代码,没有修改…

踏破铁鞋终于在一篇blog中(http://www.cnblogs.com/woaixingxing/p/5563171.html)发现了问题,原来是昨晚测试机自动升级至7.0了
修改代码

    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(height);
        }
        if (!this.isShowing()) {
            this.showAsDropDown(anchor, 0, 0);
        } else {
            this.dismiss();
        }
    }

运行ok了,在此记录一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值