一.前言
popupWindow是我们常用的控件了,我们经常会用它来实现一些弹框需求.
然而我们的产品很可能突然会对弹框外部的某些按钮的点击做出响应而且还不能让当前popupWindow消失,此时我们内心是崩溃的,真是悔不当初啊,为什么当初不干脆使用view布局引入的形式来做呢?
那么有没有一种办法让我们的popupWindow快速切换到View布局形式呢?我们来看看项目的例子
二.实现
1.核心代码
/**
* 已view引入的形式模拟popupwindow
*/
public abstract class BaseCustomPopupWindow {
protected Context mContext;
protected LayoutInflater inflater;
protected View view;
private boolean isShowing;
private ViewGroup vShowPW;
public BaseCustomPopupWindow(Context context) {
mContext = context;
inflater = LayoutInflater.from(context);
}
/**
* 隐藏
*/
public void dismiss() {
dismiss(0);
}
/**
* 隐藏
*/
public void dismiss(int anim) {
isShowing = false;
if (anim != 0) {
Animation animation = AnimationUtils.loadAnimation(mContext, anim);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
vShowPW.removeAllViews();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
vShowPW.startAnimation(animation);
} else {
vShowPW.removeAllViews();
}
if(listener != null) {
listener.onDismiss(true);
}
}
public boolean isShowing() {
return isShowing;
}
public void showAsDropDown(ViewGroup vShowPW) {
showAsDropDown(vShowPW, 0);
}
public void showAsDropDown(ViewGroup vShowPW, int anim) {
this.vShowPW = vShowPW;
isShowing = true;
vShowPW.removeAllViews();
vShowPW.addView(view);
if (anim != 0) {
Animation animation = AnimationUtils.loadAnimation(mContext, anim);
vShowPW.startAnimation(animation);
}
if(listener != null){
listener.onDismiss(false);
}
}
public void autoAsDropDown(ViewGroup vShowPW) {
autoAsDropDown(vShowPW,0);
}
public void autoAsDropDown(ViewGroup vShowPW, int anim) {
if (isShowing) {
dismiss(anim);
} else {
showAsDropDown(vShowPW,anim);
}
}
public interface onPopupWindowDismissListener {
void onDismiss(boolean isDimiss);
}
private onPopupWindowDismissListener listener;
public void setOnPopupWindowDismissListener(onPopupWindowDismissListener listener) {
this.listener = listener;
}
}
2.popupWindow代码改动
我们通过svn的代码比对来看一个实际的例子
3.调用代码改动
调用代码改动还是有一些的
首先我们要修改xml布局,未我们的view添加一个位置比如这样
<FrameLayout
android:id="@+id/v_show_pw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_0000" />
然后调用时候
indexPopupWindow.showAsDropDown(llIndex, 0, -1234);
改为
indexPopupWindow.showAsDropDown(v_show_pw);
三.最后
总的来说对我们代码改动并不大,欢迎大家评论拍砖