AbstractPopup.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package de.tudarmstadt.informatik.hostage.ui2.popup;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Rect;
  5. import android.util.DisplayMetrics;
  6. import android.view.Gravity;
  7. import android.view.LayoutInflater;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.view.Window;
  11. import android.view.WindowManager;
  12. import android.widget.LinearLayout;
  13. import android.widget.PopupWindow;
  14. import de.tudarmstadt.informatik.hostage.R;
  15. /**
  16. * Created by Julien on 13.02.14.
  17. */
  18. public abstract class AbstractPopup {
  19. public interface OnPopupItemClickListener {
  20. public void onItemClick(Object data);
  21. }
  22. private PopupWindow popupWindow;
  23. private Activity context;
  24. private OnPopupItemClickListener onPopupItemClickListener;
  25. private LinearLayout rootView;
  26. private LayoutInflater lInf;
  27. abstract public int getLayoutId();
  28. abstract void configureView(View view);
  29. public AbstractPopup(Context context, OnPopupItemClickListener listener) {
  30. super();
  31. this.context = (Activity) context;
  32. this.onPopupItemClickListener = listener;
  33. this.popupWindow = new PopupWindow(context);
  34. this.popupWindow.setOutsideTouchable(true);
  35. this.popupWindow.setFocusable(true);
  36. this.lInf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  37. }
  38. public void addItem(final AbstractPopupItem item) {
  39. View view = item.getItemView();
  40. if (this.rootView == null){
  41. this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
  42. this.configureView(this.rootView);
  43. }
  44. if (this.rootView != null){
  45. this.rootView.addView(view);
  46. view.setOnTouchListener(new View.OnTouchListener() {
  47. @Override
  48. public boolean onTouch(View view, MotionEvent event) {
  49. if(event.getAction() == MotionEvent.ACTION_DOWN){
  50. item.onItemDown(event);
  51. } else if (event.getAction() == MotionEvent.ACTION_UP){
  52. item.onItemUp(event);
  53. AbstractPopup.this.onPopupItemClickListener.onItemClick(item.onClickedResult(event));
  54. AbstractPopup.this.popupWindow.dismiss();
  55. }
  56. return true;
  57. }
  58. });
  59. }
  60. }
  61. public View getPopupView(){
  62. if (this.rootView == null){
  63. this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
  64. }
  65. return this.rootView;
  66. }
  67. public void showOnView(final View anchorView) {
  68. if (this.rootView == null){
  69. this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
  70. }
  71. if (this.rootView != null){
  72. AbstractPopup.this.popupWindow.dismiss();
  73. this.popupWindow.setContentView(this.rootView);
  74. final Rect windowFrame= new Rect();
  75. Window window = this.context.getWindow();
  76. window.getDecorView().getWindowVisibleDisplayFrame(windowFrame);
  77. final int[] position = new int[2];
  78. anchorView.getLocationOnScreen(position);
  79. final int anchorWidth = anchorView.getWidth();
  80. this.rootView.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
  81. int width = this.rootView.getMeasuredWidth();
  82. int height = this.rootView.getMeasuredHeight();
  83. int x = position[0] + (anchorWidth / 2) - (width / 2);
  84. int y = position[1] - height;
  85. x = Math.min(0,x);
  86. x = Math.max(windowFrame.width() - width, x);
  87. y = Math.min(0,y);
  88. y = Math.max(windowFrame.height() - height, y);
  89. AbstractPopup.this.configureView(this.rootView);
  90. int smallBottomOffset = 45;
  91. this.popupWindow.setWidth(width);
  92. this.popupWindow.setHeight(height);
  93. this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);
  94. /*
  95. // TO SET THE REAL SIZE
  96. ViewTreeObserver viewTreeObserver = this.rootView.getViewTreeObserver();
  97. if (viewTreeObserver != null && viewTreeObserver.isAlive()) {
  98. viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  99. @Override
  100. public void onGlobalLayout() {
  101. ViewTreeObserver observer= AbstractPopup.this.rootView.getViewTreeObserver();
  102. if (observer != null)observer.removeOnGlobalLayoutListener(this);
  103. View rootView = AbstractPopup.this.rootView;
  104. int width = rootView.getWidth();
  105. int height = rootView.getHeight();
  106. int x = position[0] + (anchorWidth / 2) - (width / 2);
  107. int y = position[1] - height;
  108. x = Math.min(0,x);
  109. x = Math.max(windowFrame.width() - width, x);
  110. y = Math.min(0,y);
  111. y = Math.max(windowFrame.height() - height, y);
  112. AbstractPopup.this.configureView(rootView);
  113. int smallBottomOffset = 10;
  114. AbstractPopup.this.popupWindow.dismiss();
  115. AbstractPopup.this.popupWindow.setWidth(width);
  116. AbstractPopup.this.popupWindow.setHeight(height + smallBottomOffset);
  117. AbstractPopup.this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);
  118. }
  119. });
  120. }
  121. */
  122. }
  123. }
  124. }