import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import com.msyc.onion.R;
import java.lang.ref.WeakReference;
/**
* 簡(jiǎn)易封裝的PopupWindow
*/
public class CustomPopupWindow implements PopupWindow.OnDismissListener {
private PopupWindow mPopupWindow;
private View contentView;
private static WeakReference<Activity> refActivity;
private Builder builder;
public CustomPopupWindow(Builder builder) {
this.builder = builder;
contentView = LayoutInflater.from(refActivity.get()).inflate(builder.contentViewId, null);
mPopupWindow = new PopupWindow(contentView, builder.width, builder.height);
if (builder.outsideTouchable) {
//設(shè)置點(diǎn)擊外部可以取消腾节,必須和下面這個(gè)方法配合才有效,(setOutsideTouchable設(shè)置生效的前提是setTouchable(true)和setFocusable(false),此處存疑)
mPopupWindow.setOutsideTouchable(true);
//設(shè)置一個(gè)空背景,設(shè)置了這個(gè)背景之后橄抹,設(shè)置點(diǎn)擊外部取消才有效
mPopupWindow.setBackgroundDrawable(new ColorDrawable()); //如果不設(shè)置PopupWindow的背景,有些版本就會(huì)出現(xiàn)一個(gè)問題:無論是點(diǎn)擊外部區(qū)域還是Back鍵都無法dismiss彈框
// mPopupWindow.setTouchable(true);
mPopupWindow.setFocusable(true); // false時(shí)PopupWindow不處理返回鍵(此處設(shè)置為true,則效果是正常的镶殷,和上方的說法存疑)
}
//Popupwindow可以點(diǎn)擊,PopupWindow彈出后,所有的觸屏和物理按鍵都有PopupWindows處理。
// 其他任何事件的響應(yīng)都必須發(fā)生在PopupWindow消失之后, (home 等系統(tǒng)層面的事件除外)响巢。
// 比如這樣一個(gè)PopupWindow出現(xiàn)的時(shí)候,按back鍵首先是讓PopupWindow消失棒妨,
// 第二次按才是退出activity踪古,
//解決Pop遮擋住虛擬鍵盤的問題
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
//讓pop自適應(yīng)輸入狀態(tài)
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
if (builder.animStyle != 0) {
mPopupWindow.setAnimationStyle(builder.animStyle); //設(shè)置pop顯示的動(dòng)畫效果
}
mPopupWindow.setOnDismissListener(this); //設(shè)置pop關(guān)閉的監(jiān)聽事件
// 原生的Outside 事件會(huì)穿透到下方(原生問題),故用gray_layout多做一層補(bǔ)充作用(主要用于點(diǎn)擊消失popwindow券腔,避免事件穿透)
if (contentView.findViewById(R.id.gray_layout) != null) {
View grayLayout = (View) contentView.findViewById(R.id.gray_layout);
grayLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
/**
* popup 消失
*/
public void dismiss() {
if (mPopupWindow != null && mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
}
/**
* 相對(duì)于窗體的顯示位置
*
* @param view 可以為Activity中的任意一個(gè)View(最終的效果一樣)批钠,
* 會(huì)通過這個(gè)View找到其父Window啡省,也就是Activity的Window。
* @param gravity 在窗體中的位置躬贡,默認(rèn)為Gravity.NO_GRAVITY
* @param x 表示距離Window邊緣的距離腰懂,方向由Gravity決定幌蚊。
* 例如:設(shè)置了Gravity.TOP翻伺,則y表示與Window上邊緣的距離畴蒲;
* 而如果設(shè)置了Gravity.BOTTOM,則y表示與下邊緣的距離商叹。
* @param y
* @return
*/
public CustomPopupWindow showAtLocation(View view, int gravity, int x, int y) {
if (mPopupWindow != null) {
mPopupWindow.showAtLocation(view, gravity, x, y);
if (builder != null) setBackgroundAlpha(builder.backgroundAlpha); //設(shè)置窗體的背景透明度為半透明
}
return this;
}
/**
* 顯示在anchor控件的正下方燕刻,或者相對(duì)這個(gè)控件的位置
*
* @param anchor 錨點(diǎn)
* @param xOff 相對(duì)這個(gè)控件x方向的偏移
* @param yOff 相對(duì)這個(gè)控件y方向的偏移
* @return
*/
// public CustomPopupWindow showAsDropDown(View anchor, int xOff, int yOff) {
// if (mPopupWindow != null) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// //7.0以上系統(tǒng)
// //獲取目標(biāo)控件在屏幕中的坐標(biāo)位置
// int[] location = new int[2];
// anchor.getLocationOnScreen(location);
// mPopupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, 0, location[1] + anchor.getHeight() + yOff);
// } else {
// mPopupWindow.showAsDropDown(anchor, xOff, yOff);
// }
// if (builder != null) setBackgroundAlpha(builder.backgroundAlpha); //設(shè)置窗體的背景透明度為半透明
// }
// return this;
// }
public CustomPopupWindow showAsDropDown(View anchor, int xOff, int yOff) {
if (mPopupWindow != null) {
if (!mPopupWindow.isShowing()) {
// 以下拉方式顯示popupwindow
mPopupWindow.showAsDropDown(anchor, xOff, yOff);
if (builder != null) setBackgroundAlpha(builder.backgroundAlpha); //設(shè)置窗體的背景透明度為半透明
}
// else {
// this.dismiss();
// }
}
return this;
}
public CustomPopupWindow showAsDropDown(View anchor) {
if (mPopupWindow != null) {
if (!mPopupWindow.isShowing()) {
// 以下拉方式顯示popupwindow
mPopupWindow.showAsDropDown(anchor);
if (builder != null) setBackgroundAlpha(builder.backgroundAlpha); //設(shè)置窗體的背景透明度為半透明
}
// else {
// this.dismiss();
// }
}
return this;
}
/**
* 根據(jù)id獲取view
*
* @param viewId
* @return
*/
public View getItemView(int viewId) {
if (mPopupWindow != null) {
return contentView.findViewById(viewId);
}
return null;
}
/**
* 根據(jù)id設(shè)置pop內(nèi)部的控件的點(diǎn)擊事件的監(jiān)聽
*
* @param viewId
* @param listener
*/
public void setOnClickListener(int viewId, View.OnClickListener listener) {
View view = getItemView(viewId);
view.setOnClickListener(listener);
}
/**
* 設(shè)置Activity或者Fragment的背景透明度
*
* @param bgAlpha 背景的透明度
*/
public void setBackgroundAlpha(float bgAlpha) {
if (refActivity.get() == null || refActivity.get().getWindow() == null) return;
WindowManager.LayoutParams layoutParams = refActivity.get().getWindow().getAttributes();
layoutParams.alpha = bgAlpha; //0.0-1.0
refActivity.get().getWindow().setAttributes(layoutParams);
}
/**
* builder 類
*/
public static class Builder {
private int contentViewId; //pop的布局文件
private int width; //pop的寬度
private int height; //pop的高度
private int animStyle; //動(dòng)畫效果
private float backgroundAlpha = 0.5f; //背景的透明度,默認(rèn)半透明
private boolean outsideTouchable = false; //設(shè)置點(diǎn)擊外部可以取消
public Builder(Activity activity) {
refActivity = new WeakReference<>(activity);
}
public Builder setContentView(int contentViewId) {
this.contentViewId = contentViewId;
return this;
}
public Builder setwidth(int width) {
this.width = width;
return this;
}
public Builder setheight(int height) {
this.height = height;
return this;
}
public Builder setAnimationStyle(int animStyle) {
this.animStyle = animStyle;
return this;
}
public Builder setBackgroundAlpha(float backgroundAlpha) {
this.backgroundAlpha = backgroundAlpha;
return this;
}
public Builder setOutsideTouchable(boolean touchable) {
outsideTouchable = touchable;
return this;
}
public CustomPopupWindow build() {
return new CustomPopupWindow(this);
}
}
@Override
public void onDismiss() {
if (builder == null || builder.backgroundAlpha != 1f)
setBackgroundAlpha(1f); //設(shè)置窗體的背景透明度為不透明
}
}
使用方法:
private CustomPopupWindow mSearchDropPop;
/**
* 初始化Pop剖笙,pop的布局是一個(gè)列表
*/
private void initPop() {
if (mSearchDropPop == null || mSearchDropAdapter == null) {
mSearchDropPop = new CustomPopupWindow.Builder(this)
.setContentView(R.layout.search_drop_pop_window)
.setwidth(LinearLayout.LayoutParams.MATCH_PARENT)
.setheight(LinearLayout.LayoutParams.WRAP_CONTENT)
.setBackgroundAlpha(1.0f)
.build();
//搜索聯(lián)想結(jié)果的列表
ListView searchLv = (ListView) mSearchDropPop.getItemView(R.id.lv_search_list);
mSearchDropAdapter = new SearchDropAdapter(this, null);
searchLv.setAdapter(mSearchDropAdapter);
searchLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mBinding.editTextSearch.setText(mSearchDropAdapter.getList().get(position).text);
mKeyword = mSearchDropAdapter.getList().get(position).text;
preSearch().search(mSearchDropAdapter.request_id); // 點(diǎn)擊聯(lián)想詞
}
});
}
}
/**關(guān)鍵字聯(lián)想詞*/
mViewModel.mSearchDropData.observe(this, result -> {
if (result == null || result.getBeanList().size() <= 0) {
mSearchDropPop.dismiss();
} else {
String s = mBinding.editTextSearch.getText().toString();
if (s.length() > 0 && s.equals(mmSearchDropWord)) {
mSearchDropAdapter.setDatas(result);
mSearchDropAdapter.notifyDataSetChanged();
mSearchDropPop.showAsDropDown(mBinding.editTextSearch);
}
}
});
@Override
protected void onDestroy() {
if (mSearchDropPop != null) {
mSearchDropPop.dismiss();
}
super.onDestroy();
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66000000"
android:orientation="vertical">
<ListView
android:id="@+id/lv_search_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:divider="@null"
android:paddingTop="@dimen/dp_6" />
<!--該View用來撐滿屏幕用 顯示灰色透明背景-->
<View
android:id="@+id/gray_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F1F2F4" />
</LinearLayout>