AbstractPopup.java 6.1 KB

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