Android Click Through PopupWindow

I have created a class that extends popupwindow. its constructor looks something like the following

super(builder.context.get());

this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setTouchInterceptor(onTouchListener);
FrameLayout frameLayout = new FrameLayout(builder.context.get());
LayoutInflater inflater = (LayoutInflater) builder.context.get().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
View overlayBase = inflater.inflate(R.layout.tutorial_view_items, null, false);
frameLayout.addView(builder.backgroundView);
frameLayout.addView(overlayBase);
setContentView(frameLayout);

the onTouchListener looks like the following:

private OnTouchListener onTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        Log.d(TAG, "Received");

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(radius, 2)) {
            Log.d(TAG, "bubbled through");
            return false;
        }
        return true;
    }
};

to actually display the popupwindow, I call SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);

Visually represented, here is the product in question

胡说

The touchListener is responding, but the input does not get sent to the underlying activity OR the popupwindow? (the button is unresponsive, where as if i were to remove the ontouchlistener, the button works fine).

update setting the touch listener to the frameLayout (ie content view of popupwindow in this case) instead of the popupwindow itself allows me to click on the buttons defined in the popup. still looking for way to delegate the touch event to the underlying activity/view

SOLUTION

Register a touch interceptor for the Popupwindow. If you want the activity to handle it, assuming you have a reference to the activity, call someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter); , if you want the popupwindow to handle the touch even, call someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor) to the popupwindow's content view , as set in the popupwindows setContentView(someView) method.

My method specifically looked like the following

private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
                radius, 2)) {
            activity.get().dispatchTouchEvent(event);
            return false;
        }
        frameLayout.dispatchTouchEvent(event);
        return true;
    }
};

SOLUTION

Register a touch interceptor for the Popupwindow. If you want the activity to handle it, assuming you have a reference to the activity, call someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter); , if you want the popupwindow to handle the touch even, call someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor) to the popupwindow's content view , as set in the popupwindows setContentView(someView) method.

My method specifically looked like the following

private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
                radius, 2)) {
            activity.get().dispatchTouchEvent(event);
            return false;
        }
        frameLayout.dispatchTouchEvent(event);
        return true;
    }
};

public class TestPopupActivity extends Activity {

//The "x" and "y" position of the "Show Button" on screen.
Point p;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   Button btn_show = (Button) findViewById(R.id.show_popup);
   btn_show.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View arg0) {

       //Open popup window
       if (p != null)
       showPopup(TestPopupActivity.this, p);
     }
   });
}

// Get the x and y position after the button is draw on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
@Override
public void onWindowFocusChanged(boolean hasFocus) {

   int[] location = new int[2];
   Button button = (Button) findViewById(R.id.show_popup);

   // Get the x, y location and store it in the location[] array
   // location[0] = x, location[1] = y.
   button.getLocationOnScreen(location);

   //Initialize the Point with x, and y positions
   p = new Point();
   p.x = location[0];
   p.y = location[1];
}

// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
   int popupWidth = 200;
   int popupHeight = 150;

   // Inflate the popup_layout.xml
   LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
   LayoutInflater layoutInflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

   // Creating the PopupWindow
   final PopupWindow popup = new PopupWindow(context);
   popup.setContentView(layout);
   popup.setWidth(popupWidth);
   popup.setHeight(popupHeight);
   popup.setFocusable(true);

   // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
   int OFFSET_X = 30;
   int OFFSET_Y = 30;

   // Clear the default translucent background
   popup.setBackgroundDrawable(new BitmapDrawable());

   // Displaying the popup at the specified location, + offsets.
   popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

   // Getting a reference to Close button, and close the popup when clicked.
   Button close = (Button) layout.findViewById(R.id.close);
   close.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
       popup.dismiss();
     }
   });
}
}

try this code may be it help you . .. . . or may be this link will help u more .. http://mrbool.com/how-to-implement-popup-window-in-android/28285

链接地址: http://www.djcxy.com/p/19154.html

上一篇: 逻辑索引如何工作?

下一篇: Android通过PopupWindow点击