popupWindow的封装使用

之前遇到需求模仿淘宝选择商品属性的功能界面,最后选择使用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);
}


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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值