PopWindow的点击事件

原创
2017/09/18 11:03
阅读数 767

自定义一个Popwindow

public class PopWindow1 extends PopupWindow {

    private WindowManager.LayoutParams params;
    private Activity context;
    public static final int SHOWDROPDOWN = 1;
    public static final int SHOWATLOCATION = 0;
    private PopupWindow popupWindow;

    public PopWindow1(final Activity content, int popLayoutId) {
        this.context = content;
        params = content.getWindow().getAttributes();
        View contentView = LayoutInflater.from(content).inflate(popLayoutId, null, true);
        popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.GRAY));
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                params.alpha = 1.0f;
                context.getWindow().setAttributes(params);
                dismiss();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void showPopWindow(View view, int type, int offX, int offY, int gravity) {
        params.alpha = 0.7f;
        context.getWindow().setAttributes(params);
        if (type == SHOWATLOCATION) {
            popupWindow.showAtLocation(view, gravity, offX, offY);
        } else if (type == SHOWDROPDOWN) {
            popupWindow.showAsDropDown(view, offX, offY, gravity);
        }
    }
}

在Activity中调用

public class PopwindowActivity extends BaseActivity {
    @Bind(R.id.popwindow1)
    Button popwindow1;
    private PopWindow1 pw;
    private View contentView;
    private WindowManager.LayoutParams params;

    @Override
    protected int getLayoutid() {
        return R.layout.activity_popwindow;
    }

    @Override
    protected void setUpView() {
        super.setUpView();
        pw = new PopWindow1(this, R.layout.popwindow_item1);
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.popwindow1)
    public void onViewClicked() {
        pw.showPopWindow(popwindow1, PopWindow1.SHOWDROPDOWN, 0, 0, Gravity.BOTTOM);
    }
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部