AbstractPopup.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 abstract LinearLayout getScrollableItemLayout();
  38. public View getRootView(){
  39. return this.rootView;
  40. }
  41. public void addItem(final AbstractPopupItem item) {
  42. View view = item.getItemView();
  43. if (this.rootView == null){
  44. this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
  45. this.configureView(this.rootView);
  46. }
  47. if (this.rootView != null){
  48. this.getScrollableItemLayout().addView(view);
  49. //this.rootView.addView(view);
  50. view.setOnTouchListener(new View.OnTouchListener() {
  51. @Override
  52. public boolean onTouch(View view, MotionEvent event) {
  53. if(event.getAction() == MotionEvent.ACTION_DOWN){
  54. item.onItemDown(event);
  55. } else if (event.getAction() == MotionEvent.ACTION_UP){
  56. item.onItemUp(event);
  57. AbstractPopup.this.onPopupItemClickListener.onItemClick(item.onClickedResult(event));
  58. AbstractPopup.this.popupWindow.dismiss();
  59. }
  60. return true;
  61. }
  62. });
  63. }
  64. }
  65. public View getPopupView(){
  66. if (this.rootView == null){
  67. this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
  68. }
  69. return this.rootView;
  70. }
  71. public void showOnView(final View anchorView) {
  72. if (this.rootView == null){
  73. this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
  74. }
  75. if (this.rootView != null){
  76. AbstractPopup.this.popupWindow.dismiss();
  77. this.popupWindow.setContentView(this.rootView);
  78. final Rect windowFrame= new Rect();
  79. Window window = this.context.getWindow();
  80. window.getDecorView().getWindowVisibleDisplayFrame(windowFrame);
  81. int orientation = this.context.getResources().getConfiguration().orientation;
  82. int windowWidth = windowFrame.width();
  83. int windowHeight = windowFrame.height();
  84. final int[] position = new int[2];
  85. anchorView.getLocationOnScreen(position);
  86. final int anchorWidth = anchorView.getWidth();
  87. this.rootView.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
  88. int width = this.rootView.getMeasuredWidth();
  89. int height = this.rootView.getMeasuredHeight();
  90. //int alh = (position[0] + width) - windowFrame.width();
  91. //if (alh < 0) alh = 0;
  92. int x = position[0] + (anchorWidth / 2) - (width / 2);
  93. int y = position[1] - height;
  94. int offset = 15;
  95. height+=offset;
  96. width = windowWidth < width ? windowWidth : width;
  97. x = Math.max(0, x);
  98. x = Math.min(windowWidth - width, x);
  99. height = windowHeight < height ? windowHeight : height;
  100. y = Math.max(0, y);
  101. y = Math.min(windowHeight - height, y);
  102. AbstractPopup.this.configureView(this.rootView);
  103. int smallBottomOffset = 45;
  104. this.popupWindow.setWidth(width);
  105. this.popupWindow.setHeight(height);
  106. this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);
  107. /*
  108. // TO SET THE REAL SIZE
  109. ViewTreeObserver viewTreeObserver = this.rootView.getViewTreeObserver();
  110. if (viewTreeObserver != null && viewTreeObserver.isAlive()) {
  111. viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  112. @Override
  113. public void onGlobalLayout() {
  114. ViewTreeObserver observer= AbstractPopup.this.rootView.getViewTreeObserver();
  115. if (observer != null)observer.removeOnGlobalLayoutListener(this);
  116. View rootView = AbstractPopup.this.rootView;
  117. int width = rootView.getWidth();
  118. int height = rootView.getHeight();
  119. int x = position[0] + (anchorWidth / 2) - (width / 2);
  120. int y = position[1] - height;
  121. x = Math.min(0,x);
  122. x = Math.max(windowFrame.width() - width, x);
  123. y = Math.min(0,y);
  124. y = Math.max(windowFrame.height() - height, y);
  125. AbstractPopup.this.configureView(rootView);
  126. int smallBottomOffset = 10;
  127. AbstractPopup.this.popupWindow.dismiss();
  128. AbstractPopup.this.popupWindow.setWidth(width);
  129. AbstractPopup.this.popupWindow.setHeight(height + smallBottomOffset);
  130. AbstractPopup.this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);
  131. }
  132. });
  133. }
  134. */
  135. }
  136. }
  137. }