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 titles) {
        mListView.setAdapter(new AdapterBase(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;
    }

}

完美使用,没有问题
PopupWindow在7.0上显示的坑_第1张图片

但今天突然发现,成了下面这个样子了
PopupWindow在7.0上显示的坑_第2张图片

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

踏破铁鞋终于在一篇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了,在此记录一下

你可能感兴趣的:(积累,分享)