AbstractPopup.java 5.5 KB

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