AbstractPopup.java 5.2 KB

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