Android PopupWindow 使用小Demo,以及从底部弹起的半透明背景的PopupWindow通用父类

关于PopupWindow的使用,网上各种介绍,不胜枚举。我这里记录一下自己写的一个小demo(备忘),背景是半透明的,从底部弹起的popupwindow框,然后封装成通用父类。效果如下:

I(img.png

下面是通用父类的代码(BasePopupWindow.java):

protected Activity activity;
protected View mContentView;
protected OnItemClickListener listener;

public BasePopupWindow(Activity activity) {
    this.activity = activity;
    initView();
}

/**
 * 初始化控件
 */
private void initView() {
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mContentView = inflater.inflate(getLayout(), null);

    bindView();
    dealEvent();

    //设置PopupWindow的View
    this.setContentView(mContentView);
    //设置PopupWindow弹出窗体的宽
    this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    //设置PopupWindow弹出窗体的高
    this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    //设置PopupWindow弹出窗体可点击
    this.setFocusable(true);
    //设置PopupWindow弹出窗体动画效果
    this.setAnimationStyle(R.style.bottom_anim);
    //关闭事件
    this.setOnDismissListener(new popupDismissLister());

    backgroundAlpha(0.5f);
    //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
    mContentView.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            int height = mContentView.findViewById(getPopupTopView()).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss();
                }
            }
            return true;
        }
    });
}

/**
 * 获取布局文件
 *
 * @return
 */
protected abstract int getLayout();

/**
 * 获取PopupWindow最高控件
 *
 * @return
 */
protected abstract int getPopupTopView();

/**
 * 绑定控件
 */
protected abstract void bindView();

/**
 * 处理相关事件
 */
protected abstract void dealEvent();

/**
 * 查找View
 *
 * @param id   控件的id
 * @param <VT> View类型
 * @return
 */
protected <VT extends View> VT getViewById(@IdRes int id) {
    return (VT) mContentView.findViewById(id);
}

/**
 * 设置popupwindow外面背景透明度
 *
 * @param bgalpha 透明度  0-1   0-透明   1-不透明
 */
private void backgroundAlpha(float bgalpha) {
    WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
    lp.alpha = bgalpha;
    activity.getWindow().setAttributes(lp);
}

/**
 * 添加子空间的监听事件
 *
 * @param listener
 */
public void addOnItemClickListener(OnItemClickListener listener) {
    this.listener = listener;
}

public interface OnItemClickListener {
    void onClicked(int flag);
}

private class popupDismissLister implements PopupWindow.OnDismissListener {
    @Override
    public void onDismiss() {

        backgroundAlpha(1f);
    }
}

使用时,只需要继承这个通用类接口,以下便是我的小例子。

RemindPopWindow.java

public final static int FLAG_EDIT = 0;
public final static int FLAG_DELETE = 1;

TextView mEdit, mDelete, mCancel;

public RemindPopWindow(Activity activity) {
    super(activity);
}

@Override
protected int getLayout() {
    return R.layout.window_remind;
}

@Override
protected int getPopupTopView() {
    return R.id.layout;
}

@Override
protected void bindView() {
    mEdit = getViewById(R.id.tv_edit);
    mDelete = getViewById(R.id.tv_delete);
    mCancel = getViewById(R.id.tv_cancel);

}

@Override
protected void dealEvent() {
    mEdit.setOnClickListener(this);
    mDelete.setOnClickListener(this);
    mCancel.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (view == mEdit) {
       if (listener!=null)listener.onClicked(FLAG_EDIT);
    }

    if (view == mDelete) {
        if (listener!=null)listener.onClicked(FLAG_DELETE);
    }
    if (view == mCancel) {
        dismiss();
    }
}

提示框的布局window_remind.xml,ViewGroup为LinearLayout ,id为layout。

<TextView
    android:id="@+id/tv_edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center"
    android:minHeight="60dp"
    android:text="编辑"
    android:textColor="#5f5f5f"
    android:textSize="16sp" />

<TextView
    android:id="@+id/tv_delete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="1px"
    android:background="@android:color/white"
    android:gravity="center"
    android:minHeight="60dp"
    android:text="删除"
    android:textColor="#5f5f5f"
    android:textSize="16sp" />

<TextView
    android:id="@+id/tv_cancel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:minHeight="60dp"
    android:text="取消"
    android:textColor="#5f5f5f"
    android:textSize="16sp" />

这样一来,提示的对话框就写好了,使用时,只要在对应的布局中显示PopupWindow就行了。

具体代码,请见:https://github.com/Henry-Mark/PopWindowDemo

初次写,如有啥问题,还请大神们指正,谢谢。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容