AbstractPopup.java 6.2 KB

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