AbstractPopup.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.View;
  8. import android.view.Window;
  9. import android.view.WindowManager;
  10. import android.widget.LinearLayout;
  11. import android.widget.PopupWindow;
  12. /**
  13. * Created by Julien on 13.02.14.
  14. */
  15. public abstract class AbstractPopup {
  16. public interface OnPopupItemClickListener {
  17. public void onItemClick(AbstractPopupItem item);
  18. }
  19. private PopupWindow popupWindow;
  20. private Activity context;
  21. private OnPopupItemClickListener onPopupItemClickListener;
  22. private LinearLayout rootView;
  23. private LayoutInflater lInf;
  24. abstract public int getLayoutId();
  25. abstract void configureView(View view);
  26. public AbstractPopup(Context context, OnPopupItemClickListener listener) {
  27. super();
  28. this.context = (Activity) context;
  29. this.onPopupItemClickListener = listener;
  30. this.popupWindow = new PopupWindow(context);
  31. this.popupWindow.setOutsideTouchable(true);
  32. this.popupWindow.setFocusable(true);
  33. this.lInf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  34. }
  35. public void addItem(final AbstractPopupItem item) {
  36. View view = item.getItemView();
  37. if (this.rootView == null){
  38. this.rootView = (LinearLayout) lInf.inflate(this.getLayoutId(), null);
  39. this.configureView(this.rootView);
  40. }
  41. if (this.rootView != null){
  42. this.rootView.addView(view);
  43. view.setOnClickListener(new View.OnClickListener() {
  44. @Override
  45. public void onClick(View view) {
  46. onPopupItemClickListener.onItemClick(item);
  47. popupWindow.dismiss();
  48. }
  49. });
  50. }
  51. }
  52. public View getPopupView(){
  53. return rootView;
  54. }
  55. public void showOnView(View view) {
  56. if (this.rootView == null){
  57. this.rootView = (LinearLayout) lInf.inflate(this.getLayoutId(), null);
  58. }
  59. popupWindow.setContentView(rootView);
  60. Rect rectagle= new Rect();
  61. Window window= this.context.getWindow();
  62. window.getDecorView().getWindowVisibleDisplayFrame(rectagle);
  63. int[] position = new int[2];
  64. view.getLocationOnScreen(position);
  65. rootView.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
  66. int x = position[0] + (view.getWidth() / 2) - (rootView.getMeasuredWidth() / 2);
  67. int y = position[1] - rootView.getMeasuredHeight();
  68. x = Math.min(0,x);
  69. x = Math.max(rectagle.width()-rootView.getMeasuredWidth(), x);
  70. y = Math.min(0,y);
  71. y = Math.max(rectagle.height()-rootView.getMeasuredHeight(), y);
  72. this.configureView(rootView);
  73. int smallBottomOffset = 10;
  74. popupWindow.setWidth(rootView.getMeasuredWidth());
  75. popupWindow.setHeight(rootView.getMeasuredHeight() + smallBottomOffset);
  76. popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y);
  77. }
  78. }