popupWindow的封装使用

来源:互联网 发布:米特网域名怎么解析 编辑:程序博客网 时间:2024/06/08 15:06

之前遇到需求模仿淘宝选择商品属性的功能界面,最后选择使用popupwindow实现,还好需要的功能都能实现


封装类


/** * @author z`guo`an on 2017/4/26 16:27 */public class PopupWindow {        public PopupWindow() {    }    public interface PopupWindowListeners {        void setPopupWindowListeners(LinearLayout layout);        void onDismiss();        void backgroundAlpha(float bgAlpha);    }    public void setPopupWindowListeners(PopupWindowListeners popupWindowListeners){        this.popupWindowListeners = popupWindowListeners;    }    private android.widget.PopupWindow popupWindow;    private PopupWindowListeners popupWindowListeners;    public void bottomwindow(View view, LinearLayout layout) {        if (popupWindow != null && popupWindow.isShowing()) {            return;        }        popupWindow = new android.widget.PopupWindow(layout,                ViewGroup.LayoutParams.MATCH_PARENT,                1200); // ViewGroup.LayoutParams.WRAP_CONTENT        //点击空白处时,隐藏掉pop窗口        popupWindow.setFocusable(true);        popupWindow.setBackgroundDrawable(new BitmapDrawable());        //添加弹出、弹入的动画        popupWindow.setAnimationStyle(R.style.Popupwindow);        int[] location = new int[2];        view.getLocationOnScreen(location);        popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]);        //添加按键事件监听        popupWindowListeners.setPopupWindowListeners(layout);        //添加pop窗口关闭事件,主要是实现关闭时改变背景的透明度        popupWindow.setOnDismissListener(new android.widget.PopupWindow.OnDismissListener() {            @Override            public void onDismiss() {                popupWindowListeners.onDismiss();            }        });        popupWindowListeners.backgroundAlpha(0.7f);    }    public void Dissmiss(){        popupWindow.dismiss();    }    public boolean isShowing(){        return popupWindow.isShowing();    }

if (window == null) {    window = new PopupWindow();}binding.messageC.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        setpopupvindow(v);    }});window.setPopupWindowListeners(new PopupWindow.PopupWindowListeners() {    @Override    public void setPopupWindowListeners(LinearLayout layout) {    }    @Override    public void onDismiss() {        WindowManager.LayoutParams params = context.getWindow().getAttributes();        params.alpha = 1f;        context.getWindow().setAttributes(params);    }    @Override    public void backgroundAlpha(float bgAlpha) {        WindowManager.LayoutParams lp = context.getWindow().getAttributes();        lp.alpha = bgAlpha; //0.0-1.0        context.getWindow().setAttributes(lp);        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);    }});

private void setpopupvindow(View view) {    LinearLayout layout = (LinearLayout) context.getLayoutInflater().inflate(R.layout.popup_window_paccont, null);    ListView lst = (ListView) layout.findViewById(R.id.listv);    TextView txv = (TextView) layout.findViewById(R.id.message_c);    txv.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            if (window.isShowing()) {                window.Dissmiss();            }        }    });    TextOneAdapter oneAdapter = new TextOneAdapter(context , getData());    lst.setAdapter(oneAdapter);    CommonUtils.fixListViewHeight(lst);    window.bottomwindow(view, layout);}


觉得学有所得,特此记录一笔





原创粉丝点击