AbstractPopup.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package de.tudarmstadt.informatik.hostage.ui.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. import de.tudarmstadt.informatik.hostage.R;
  14. /**
  15. * Created by Julien on 13.02.14.
  16. */
  17. public abstract class AbstractPopup {
  18. //static final int ORIENTATION_LANDSCAPE = 2;
  19. /**
  20. * OnPopupItemClickListener
  21. * The listener will be called if the user selects a table row
  22. */
  23. public interface OnPopupItemClickListener {
  24. public void onItemClick(Object data);
  25. }
  26. private PopupWindow popupWindow;
  27. private Activity context;
  28. private OnPopupItemClickListener onPopupItemClickListener;
  29. private LinearLayout rootView;
  30. private LayoutInflater lInf;
  31. private View lastItemView;
  32. /**
  33. * Override to return the layout id.
  34. * @return int layoutID
  35. */
  36. abstract public int getLayoutId();
  37. /**
  38. * Override to make additional stuff with the rootview.
  39. * @param View rootview
  40. */
  41. abstract void configureView(View view);
  42. /**
  43. * Constructor
  44. * @param Context context
  45. * @param OnPopupItemClickListener listener
  46. */
  47. public AbstractPopup(Context context, OnPopupItemClickListener listener) {
  48. super();
  49. this.context = (Activity) context;
  50. this.onPopupItemClickListener = listener;
  51. this.popupWindow = new PopupWindow(context);
  52. this.popupWindow.setOutsideTouchable(true);
  53. this.popupWindow.setFocusable(true);
  54. this.lInf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  55. }
  56. /**
  57. * Override to return a linear layout to add a scrollview.
  58. * @return LinearLayout
  59. */
  60. public abstract LinearLayout getScrollableItemLayout();
  61. /**
  62. * Returns the root view
  63. * @return View the rootview
  64. */
  65. public View getRootView(){
  66. return this.rootView;
  67. }
  68. /**
  69. * Adds a table row item.
  70. * @param AbstractPopupItem item
  71. */
  72. public void addItem(final AbstractPopupItem item) {
  73. View view = item.getRootView();
  74. if (this.rootView == null){
  75. this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
  76. this.configureView(this.rootView);
  77. }
  78. if (this.rootView != null){
  79. this.getScrollableItemLayout().addView(view);
  80. lastItemView = view;
  81. //this.rootView.addView(view);
  82. view.setOnTouchListener(new View.OnTouchListener() {
  83. @Override
  84. public boolean onTouch(View view, MotionEvent event) {
  85. if(event.getAction() == MotionEvent.ACTION_DOWN){
  86. item.onItemSelect(event);
  87. } else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_MOVE){
  88. item.onItemDeselect(event);
  89. } else if (event.getAction() == MotionEvent.ACTION_UP){
  90. item.onItemDeselect(event);
  91. AbstractPopup.this.onPopupItemClickListener.onItemClick(item.onClickedResult(event));
  92. AbstractPopup.this.popupWindow.dismiss();
  93. }
  94. return true;
  95. }
  96. });
  97. }
  98. }
  99. /**
  100. * Returns the rootview.
  101. * If the root view is null, it initialises it with the layout id.
  102. * @return View the root view
  103. */
  104. public View getPopupView(){
  105. if (this.rootView == null){
  106. this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
  107. }
  108. return this.rootView;
  109. }
  110. /**
  111. * Opens the Popup View on top of the given anchor.
  112. * @param View anchorView
  113. */
  114. public void showOnView(final View anchorView) {
  115. if (this.rootView == null){
  116. this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
  117. }
  118. if (this.rootView != null){
  119. AbstractPopup.this.popupWindow.dismiss();
  120. this.popupWindow.setContentView(this.rootView);
  121. final Rect windowFrame= new Rect();
  122. Window window = this.context.getWindow();
  123. window.getDecorView().getWindowVisibleDisplayFrame(windowFrame);
  124. //int orientation = this.context.getResources().getConfiguration().orientation;
  125. int windowWidth = windowFrame.width();
  126. int windowHeight = windowFrame.height();
  127. final int[] position = new int[2];
  128. anchorView.getLocationOnScreen(position);
  129. final int anchorWidth = anchorView.getWidth();
  130. this.rootView.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
  131. int width = this.rootView.getMeasuredWidth();
  132. int height = this.rootView.getMeasuredHeight();
  133. //int alh = (position[0] + width) - windowFrame.width();
  134. //if (alh < 0) alh = 0;
  135. int offset = windowFrame.top;
  136. int x = position[0] + (anchorWidth / 2) - (width / 2);
  137. int y = (position[1] - height) + offset;
  138. height+=(offset/2);
  139. width = windowWidth < width ? windowWidth : width;
  140. x = Math.max(0, x);
  141. x = Math.min(windowWidth - width, x);
  142. height = (windowHeight < height ? windowHeight : height) - 10;
  143. y = Math.max(0, y);
  144. y = Math.min(windowHeight - height, y);
  145. AbstractPopup.this.configureView(this.rootView);
  146. int smallBottomOffset = 45;
  147. this.popupWindow.setWidth(width);
  148. this.popupWindow.setHeight(height);
  149. if(lastItemView != null){
  150. View v = lastItemView.findViewById(R.id.bottom_seperator);
  151. if(v != null){
  152. v.setVisibility(View.GONE);
  153. }
  154. }
  155. this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);
  156. }
  157. }
  158. }