|
@@ -5,6 +5,7 @@ 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;
|
|
@@ -17,7 +18,7 @@ import android.widget.PopupWindow;
|
|
|
public abstract class AbstractPopup {
|
|
|
|
|
|
public interface OnPopupItemClickListener {
|
|
|
- public void onItemClick(AbstractPopupItem item);
|
|
|
+ public void onItemClick(Object data);
|
|
|
}
|
|
|
|
|
|
private PopupWindow popupWindow;
|
|
@@ -44,16 +45,19 @@ public abstract class AbstractPopup {
|
|
|
View view = item.getItemView();
|
|
|
|
|
|
if (this.rootView == null){
|
|
|
- this.rootView = (LinearLayout) lInf.inflate(this.getLayoutId(), null);
|
|
|
+ this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
|
|
|
this.configureView(this.rootView);
|
|
|
}
|
|
|
if (this.rootView != null){
|
|
|
this.rootView.addView(view);
|
|
|
- view.setOnClickListener(new View.OnClickListener() {
|
|
|
+ view.setOnTouchListener(new View.OnTouchListener() {
|
|
|
@Override
|
|
|
- public void onClick(View view) {
|
|
|
- onPopupItemClickListener.onItemClick(item);
|
|
|
- popupWindow.dismiss();
|
|
|
+ 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;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -61,43 +65,89 @@ public abstract class AbstractPopup {
|
|
|
}
|
|
|
|
|
|
public View getPopupView(){
|
|
|
- return rootView;
|
|
|
+ if (this.rootView == null){
|
|
|
+ this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
|
|
|
+ }
|
|
|
+ return this.rootView;
|
|
|
}
|
|
|
|
|
|
- public void showOnView(View view) {
|
|
|
+ public void showOnView(final View anchorView) {
|
|
|
if (this.rootView == null){
|
|
|
- this.rootView = (LinearLayout) lInf.inflate(this.getLayoutId(), 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;
|
|
|
|
|
|
- popupWindow.setContentView(rootView);
|
|
|
+ x = Math.min(0,x);
|
|
|
+ x = Math.max(windowFrame.width() - width, x);
|
|
|
|
|
|
- Rect rectagle= new Rect();
|
|
|
- Window window= this.context.getWindow();
|
|
|
- window.getDecorView().getWindowVisibleDisplayFrame(rectagle);
|
|
|
+ y = Math.min(0,y);
|
|
|
+ y = Math.max(windowFrame.height() - height, y);
|
|
|
|
|
|
- int[] position = new int[2];
|
|
|
- view.getLocationOnScreen(position);
|
|
|
+ AbstractPopup.this.configureView(this.rootView);
|
|
|
|
|
|
- rootView.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
|
|
|
+ int smallBottomOffset = 10;
|
|
|
+ this.popupWindow.setWidth(width);
|
|
|
+ this.popupWindow.setHeight(height + smallBottomOffset);
|
|
|
+ this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);
|
|
|
|
|
|
- int x = position[0] + (view.getWidth() / 2) - (rootView.getMeasuredWidth() / 2);
|
|
|
- int y = position[1] - rootView.getMeasuredHeight();
|
|
|
|
|
|
- x = Math.min(0,x);
|
|
|
- x = Math.max(rectagle.width()-rootView.getMeasuredWidth(), x);
|
|
|
|
|
|
- y = Math.min(0,y);
|
|
|
- y = Math.max(rectagle.height()-rootView.getMeasuredHeight(), y);
|
|
|
+ /*
|
|
|
+ // 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);
|
|
|
|
|
|
- this.configureView(rootView);
|
|
|
+ View rootView = AbstractPopup.this.rootView;
|
|
|
|
|
|
- int smallBottomOffset = 10;
|
|
|
+ int width = rootView.getWidth();
|
|
|
+ int height = rootView.getHeight();
|
|
|
|
|
|
- popupWindow.setWidth(rootView.getMeasuredWidth());
|
|
|
- popupWindow.setHeight(rootView.getMeasuredHeight() + smallBottomOffset);
|
|
|
+ int x = position[0] + (anchorWidth / 2) - (width / 2);
|
|
|
+ int y = position[1] - height;
|
|
|
|
|
|
- popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y);
|
|
|
+ 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);
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ */
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|