|
@@ -0,0 +1,104 @@
|
|
|
+package com.sunwin.visitorapp.utils;
|
|
|
+
|
|
|
+import android.content.res.Resources;
|
|
|
+import android.graphics.Color;
|
|
|
+import android.graphics.drawable.ColorDrawable;
|
|
|
+import android.view.Gravity;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.PopupWindow;
|
|
|
+
|
|
|
+import com.sunwin.visitorapp.R;
|
|
|
+
|
|
|
+public class PopupWindowHelper {
|
|
|
+
|
|
|
+ private View popupView;
|
|
|
+ private PopupWindow mPopupWindow;
|
|
|
+ private static final int TYPE_WRAP_CONTENT = 0, TYPE_MATCH_PARENT = 1;
|
|
|
+
|
|
|
+ public PopupWindowHelper(View view) {
|
|
|
+ popupView = view;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showAsDropDown(View anchor) {
|
|
|
+ initPopupWindow(TYPE_WRAP_CONTENT);
|
|
|
+ mPopupWindow.showAsDropDown(anchor);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showAsDropDown(View anchor, int xoff, int yoff) {
|
|
|
+ initPopupWindow(TYPE_WRAP_CONTENT);
|
|
|
+ mPopupWindow.showAsDropDown(anchor, xoff, yoff);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showAtLocation(View parent, int gravity, int x, int y) {
|
|
|
+ initPopupWindow(TYPE_WRAP_CONTENT);
|
|
|
+ mPopupWindow.showAtLocation(parent, gravity, x, y);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void dismiss() {
|
|
|
+ if (mPopupWindow.isShowing()) {
|
|
|
+ mPopupWindow.dismiss();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showAsPopUp(View anchor) {
|
|
|
+ showAsPopUp(anchor, 0, 0,Gravity.TOP);
|
|
|
+ }
|
|
|
+ public void showAsPopDown(View anchor) {
|
|
|
+ showAsPopUp(anchor, 0, 0,Gravity.BOTTOM);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showAsPopUp(View anchor, int xoff, int yoff,int topDown) {
|
|
|
+ initPopupWindow(TYPE_WRAP_CONTENT);
|
|
|
+ mPopupWindow.setWidth(anchor.getMeasuredWidth());
|
|
|
+// mPopupWindow.setAnimationStyle(R.style.dialog_with_alpha);
|
|
|
+ popupView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
|
+ int height = popupView.getMeasuredHeight();
|
|
|
+ int[] location = new int[2];
|
|
|
+ anchor.getLocationInWindow(location);
|
|
|
+ mPopupWindow.showAtLocation(anchor, Gravity.LEFT | topDown, location[0] - xoff, location[1] - height - yoff);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showFromBottom(View anchor) {
|
|
|
+ initPopupWindow(TYPE_MATCH_PARENT);
|
|
|
+ mPopupWindow.setAnimationStyle(R.style.dialog_with_alpha);
|
|
|
+ mPopupWindow.showAtLocation(anchor, Gravity.LEFT | Gravity.BOTTOM, 0, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showFromTop(View anchor) {
|
|
|
+ initPopupWindow(TYPE_MATCH_PARENT);
|
|
|
+ mPopupWindow.setAnimationStyle(R.style.dialog_with_alpha);
|
|
|
+ mPopupWindow.showAtLocation(anchor, Gravity.LEFT | Gravity.TOP, 0, getStatusBarHeight());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * touch outside dismiss the popupwindow, default is ture
|
|
|
+ *
|
|
|
+ * @param isCancelable
|
|
|
+ */
|
|
|
+ public void setCancelable(boolean isCancelable) {
|
|
|
+ if (isCancelable) {
|
|
|
+ mPopupWindow.setOutsideTouchable(true);
|
|
|
+ mPopupWindow.setFocusable(true);
|
|
|
+ } else {
|
|
|
+ mPopupWindow.setOutsideTouchable(false);
|
|
|
+ mPopupWindow.setFocusable(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void initPopupWindow(int type) {
|
|
|
+ if (type == TYPE_WRAP_CONTENT) {
|
|
|
+
|
|
|
+ mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
|
+
|
|
|
+ } else if (type == TYPE_MATCH_PARENT) {
|
|
|
+ mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
|
+ }
|
|
|
+ mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#dedede")));
|
|
|
+ setCancelable(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ private int getStatusBarHeight() {
|
|
|
+ return Math.round(25 * Resources.getSystem().getDisplayMetrics().density);
|
|
|
+ }
|
|
|
+}
|