123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package de.tudarmstadt.informatik.hostage.ui2.popup;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Rect;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.LinearLayout;
- import android.widget.PopupWindow;
- /**
- * Created by Julien on 13.02.14.
- */
- public abstract class AbstractPopup {
- public interface OnPopupItemClickListener {
- public void onItemClick(Object data);
- }
- private PopupWindow popupWindow;
- private Activity context;
- private OnPopupItemClickListener onPopupItemClickListener;
- private LinearLayout rootView;
- private LayoutInflater lInf;
- abstract public int getLayoutId();
- abstract void configureView(View view);
- public AbstractPopup(Context context, OnPopupItemClickListener listener) {
- super();
- this.context = (Activity) context;
- this.onPopupItemClickListener = listener;
- this.popupWindow = new PopupWindow(context);
- this.popupWindow.setOutsideTouchable(true);
- this.popupWindow.setFocusable(true);
- this.lInf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- public void addItem(final AbstractPopupItem item) {
- View view = item.getItemView();
- if (this.rootView == null){
- this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
- this.configureView(this.rootView);
- }
- if (this.rootView != null){
- this.rootView.addView(view);
- view.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View view, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_UP){
- AbstractPopup.this.onPopupItemClickListener.onItemClick(item.onClickedResult(event));
- AbstractPopup.this.popupWindow.dismiss();
- }
- return true;
- }
- });
- }
- }
- public View getPopupView(){
- if (this.rootView == null){
- this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
- }
- return this.rootView;
- }
- public void showOnView(final View anchorView) {
- if (this.rootView == null){
- this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
- }
- if (this.rootView != null){
- AbstractPopup.this.popupWindow.dismiss();
- this.popupWindow.setContentView(this.rootView);
- final Rect windowFrame= new Rect();
- Window window = this.context.getWindow();
- window.getDecorView().getWindowVisibleDisplayFrame(windowFrame);
- final int[] position = new int[2];
- anchorView.getLocationOnScreen(position);
- final int anchorWidth = anchorView.getWidth();
- this.rootView.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
- int width = this.rootView.getMeasuredWidth();
- int height = this.rootView.getMeasuredHeight();
- int x = position[0] + (anchorWidth / 2) - (width / 2);
- int y = position[1] - height;
- x = Math.min(0,x);
- x = Math.max(windowFrame.width() - width, x);
- y = Math.min(0,y);
- y = Math.max(windowFrame.height() - height, y);
- AbstractPopup.this.configureView(this.rootView);
- int smallBottomOffset = 10;
- this.popupWindow.setWidth(width);
- this.popupWindow.setHeight(height + smallBottomOffset);
- this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);
- /*
- // TO SET THE REAL SIZE
- ViewTreeObserver viewTreeObserver = this.rootView.getViewTreeObserver();
- if (viewTreeObserver != null && viewTreeObserver.isAlive()) {
- viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
- @Override
- public void onGlobalLayout() {
- ViewTreeObserver observer= AbstractPopup.this.rootView.getViewTreeObserver();
- if (observer != null)observer.removeOnGlobalLayoutListener(this);
- View rootView = AbstractPopup.this.rootView;
- int width = rootView.getWidth();
- int height = rootView.getHeight();
- int x = position[0] + (anchorWidth / 2) - (width / 2);
- int y = position[1] - height;
- x = Math.min(0,x);
- x = Math.max(windowFrame.width() - width, x);
- y = Math.min(0,y);
- y = Math.max(windowFrame.height() - height, y);
- AbstractPopup.this.configureView(rootView);
- int smallBottomOffset = 10;
- AbstractPopup.this.popupWindow.dismiss();
- AbstractPopup.this.popupWindow.setWidth(width);
- AbstractPopup.this.popupWindow.setHeight(height + smallBottomOffset);
- AbstractPopup.this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);
- }
- });
- }
- */
- }
- }
- }
|