|
|
@@ -0,0 +1,181 @@
|
|
|
+package com.sheep.gamegroup.util.viewHelper;
|
|
|
+
|
|
|
+import android.app.Activity;
|
|
|
+import android.graphics.Color;
|
|
|
+import android.graphics.drawable.ColorDrawable;
|
|
|
+import android.support.annotation.LayoutRes;
|
|
|
+import android.support.v4.widget.PopupWindowCompat;
|
|
|
+import android.view.Gravity;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.view.WindowManager;
|
|
|
+import android.widget.PopupWindow;
|
|
|
+
|
|
|
+import com.sheep.gamegroup.absBase.AbsObserver;
|
|
|
+import com.sheep.gamegroup.util.ViewUtil;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by realicing on 2019/3/26.
|
|
|
+ * realicing@sina.com
|
|
|
+ */
|
|
|
+public class PopupWindowUtil {
|
|
|
+ public static void delayShowLayout(Activity activity, PopupWindowParams popupWindowParams) {
|
|
|
+ ViewUtil.delay2(new AbsObserver<Integer>(){
|
|
|
+ @Override
|
|
|
+ public void onNext(Integer integer) {
|
|
|
+ PopupWindow popWindow = PopupWindowUtil.showLayout(activity, popupWindowParams);
|
|
|
+ if(popupWindowParams.getShowTime() > 0) {
|
|
|
+ ViewUtil.delay2(new AbsObserver<Integer>() {
|
|
|
+ @Override
|
|
|
+ public void onNext(Integer integer) {
|
|
|
+ try {
|
|
|
+ popWindow.dismiss();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, popupWindowParams.getShowTime());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, popupWindowParams.getDelay());
|
|
|
+
|
|
|
+ }
|
|
|
+ public static PopupWindow showLayout(Activity activity, PopupWindowParams popupWindowParams) {
|
|
|
+ // 用于PopupWindow的View
|
|
|
+ View contentView= LayoutInflater.from(activity).inflate(popupWindowParams.layoutId, null, false);
|
|
|
+ // 创建PopupWindow对象,其中:
|
|
|
+ // 第一个参数是用于PopupWindow中的View,第二个参数是PopupWindow的宽度,
|
|
|
+ // 第三个参数是PopupWindow的高度,第四个参数指定PopupWindow能否获得焦点
|
|
|
+ PopupWindow window=new PopupWindow(contentView, popupWindowParams.width, popupWindowParams.height);
|
|
|
+ // 设置PopupWindow的背景
|
|
|
+ window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
|
|
+ // 设置PopupWindow是否能响应外部点击事件
|
|
|
+ window.setOutsideTouchable(popupWindowParams.outsideTouchable);
|
|
|
+ // 设置PopupWindow是否能响应点击事件
|
|
|
+ window.setTouchable(popupWindowParams.focusable);
|
|
|
+ // 显示PopupWindow,其中:
|
|
|
+ // 第一个参数是PopupWindow的锚点,第二和第三个参数分别是PopupWindow相对锚点的x、y偏移
|
|
|
+ try {
|
|
|
+ PopupWindowCompat.showAsDropDown(window, popupWindowParams.anchor, popupWindowParams.xoff, popupWindowParams.yoff, popupWindowParams.gravity);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 或者也可以调用此方法显示PopupWindow,其中:
|
|
|
+ // 第一个参数是PopupWindow的父View,第二个参数是PopupWindow相对父View的位置,
|
|
|
+ // 第三和第四个参数分别是PopupWindow相对父View的x、y偏移
|
|
|
+ // window.showAtLocation(parent, gravity, x, y);
|
|
|
+ return window;
|
|
|
+ }
|
|
|
+ public static class PopupWindowParams {
|
|
|
+ private @LayoutRes int layoutId;
|
|
|
+ private long delay;
|
|
|
+ private long showTime;
|
|
|
+ private int width = WindowManager.LayoutParams.WRAP_CONTENT;
|
|
|
+ private int height = WindowManager.LayoutParams.WRAP_CONTENT;
|
|
|
+ private View anchor;
|
|
|
+ private int xoff = 0;
|
|
|
+ private int yoff = 0;
|
|
|
+ private boolean outsideTouchable = false;
|
|
|
+ private boolean focusable = true;
|
|
|
+ private int gravity = Gravity.NO_GRAVITY;
|
|
|
+
|
|
|
+ public PopupWindowParams(int layoutId) {
|
|
|
+ this.layoutId = layoutId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getLayoutId() {
|
|
|
+ return layoutId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getDelay() {
|
|
|
+ return delay;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setDelay(long delay) {
|
|
|
+ this.delay = delay;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getShowTime() {
|
|
|
+ return showTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setShowTime(long showTime) {
|
|
|
+ this.showTime = showTime;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getWidth() {
|
|
|
+ return width;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setWidth(int width) {
|
|
|
+ this.width = width;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getHeight() {
|
|
|
+ return height;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setHeight(int height) {
|
|
|
+ this.height = height;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public View getAnchor() {
|
|
|
+ return anchor;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setAnchor(View anchor) {
|
|
|
+ this.anchor = anchor;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getXoff() {
|
|
|
+ return xoff;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setXoff(int xoff) {
|
|
|
+ this.xoff = xoff;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getYoff() {
|
|
|
+ return yoff;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setYoff(int yoff) {
|
|
|
+ this.yoff = yoff;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isFocusable() {
|
|
|
+ return focusable;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setFocusable(boolean focusable) {
|
|
|
+ this.focusable = focusable;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isOutsideTouchable() {
|
|
|
+ return outsideTouchable;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setOutsideTouchable(boolean outsideTouchable) {
|
|
|
+ this.outsideTouchable = outsideTouchable;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getGravity() {
|
|
|
+ return gravity;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindowParams setGravity(int gravity) {
|
|
|
+ this.gravity = gravity;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|