Android PopupWindow 使用小Demo,以及從底部彈起的半透明背景的PopupWindow通用父類

關(guān)于PopupWindow的使用,網(wǎng)上各種介紹,不勝枚舉。我這里記錄一下自己寫的一個小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();

    //設(shè)置PopupWindow的View
    this.setContentView(mContentView);
    //設(shè)置PopupWindow彈出窗體的寬
    this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    //設(shè)置PopupWindow彈出窗體的高
    this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    //設(shè)置PopupWindow彈出窗體可點擊
    this.setFocusable(true);
    //設(shè)置PopupWindow彈出窗體動畫效果
    this.setAnimationStyle(R.style.bottom_anim);
    //關(guān)閉事件
    this.setOnDismissListener(new popupDismissLister());

    backgroundAlpha(0.5f);
    //mMenuView添加OnTouchListener監(jiān)聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
    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();

/**
 * 處理相關(guān)事件
 */
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);
}

/**
 * 設(shè)置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);
}

/**
 * 添加子空間的監(jiān)聽事件
 *
 * @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" />

這樣一來,提示的對話框就寫好了,使用時,只要在對應(yīng)的布局中顯示PopupWindow就行了。

具體代碼,請見:https://github.com/Henry-Mark/PopWindowDemo。

初次寫,如有啥問題,還請大神們指正,謝謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容