Skip to content

Commit

Permalink
add OnAttachedWindowListener callback
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyoona7 committed Sep 14, 2017
1 parent fa5c665 commit 6b5469f
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lib/src/main/java/com/zyyoona7/lib/EasyPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StyleRes;
import android.support.v4.widget.PopupWindowCompat;
Expand Down Expand Up @@ -90,6 +89,8 @@ public class EasyPopup implements PopupWindow.OnDismissListener {
//getViewTreeObserver监听时
private boolean isOnlyGetWH = true;

private OnAttachedWindowListener mOnAttachedWindowListener;


public EasyPopup(Context context) {
this.mContext = context;
Expand Down Expand Up @@ -457,6 +458,7 @@ public void showAtAnchorView(@NonNull View anchor, @VerticalGravity final int ve

x = calculateX(anchor, horizGravity, measuredW, x);
y = calculateY(anchor, vertGravity, measuredH, y);
Log.i(TAG, "showAtAnchorView: w=" + measuredW + ",y=" + measuredH);
PopupWindowCompat.showAsDropDown(mPopupWindow, anchor, x, y, Gravity.NO_GRAVITY);
}

Expand Down Expand Up @@ -549,6 +551,7 @@ private void updateLocation(int width, int height, @NonNull View anchor, @Vertic
final int measuredH = height;
x = calculateX(anchor, horizGravity, measuredW, x);
y = calculateY(anchor, vertGravity, measuredH, y);
Log.i(TAG, "updateLocation: x=" + measuredW + ",y=" + measuredH);
mPopupWindow.update(anchor, x, y, width, height);
}

Expand All @@ -558,6 +561,10 @@ private void updateLocation(int width, int height, @NonNull View anchor, @Vertic
public void onGlobalLayout() {
mWidth = getContentView().getWidth();
mHeight = getContentView().getHeight();
//回调
if (mOnAttachedWindowListener != null) {
mOnAttachedWindowListener.onAttachedWindow(mWidth, mHeight, EasyPopup.this);
}
//只获取宽高时,不执行更新操作
if (isOnlyGetWH) {
removeGlobalLayoutListener();
Expand All @@ -581,6 +588,11 @@ public <T extends EasyPopup> T setOnDismissListener(PopupWindow.OnDismissListene
return (T) this;
}

public <T extends EasyPopup> T setOnAttachedWindowListener(OnAttachedWindowListener listener) {
this.mOnAttachedWindowListener = listener;
return (T) this;
}

/**
* 处理背景变暗
* https://blog.nex3z.com/2016/12/04/%E5%BC%B9%E5%87%BApopupwindow%E5%90%8E%E8%AE%A9%E8%83%8C%E6%99%AF%E5%8F%98%E6%9A%97%E7%9A%84%E6%96%B9%E6%B3%95/
Expand Down Expand Up @@ -752,4 +764,20 @@ private void removeGlobalLayoutListener() {
}
}

/**
* PopupWindow是否显示在window中
* 用于获取准确的PopupWindow宽高,可以重新设置偏移量
*/
public interface OnAttachedWindowListener {

/**
* 在 show方法之后 updateLocation之前执行
*
* @param width PopupWindow准确的宽
* @param height PopupWindow准确的高
* @param easyPop
*/
void onAttachedWindow(int width, int height, EasyPopup easyPop);
}

}

0 comments on commit 6b5469f

Please sign in to comment.