弹出窗口关闭外部触摸不工作

Nit*_*ish 1 android android-popupwindow

请先检查我到目前为止所做的尝试,然后再进行复制.
我试图在外部触摸时关闭弹出窗口.我已经尝试了所有可用的解决方案,但它不起作用.

第一次尝试:

pwindo.setBackgroundDrawable(new BitmapDrawable());
            pwindo.setFocusable(true);
            pwindo.setOutsideTouchable(true);
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

pwindo.setBackgroundDrawable(new ColorDrawable());
            pwindo.setFocusable(true);
            pwindo.setOutsideTouchable(true);
Run Code Online (Sandbox Code Playgroud)

第3次尝试:

pwindo.setBackgroundDrawable(new ColorDrawable());
            pwindo.setFocusable(true);
            pwindo.setOutsideTouchable(false);
Run Code Online (Sandbox Code Playgroud)

第四次尝试:

        pwindo.setFocusable(true);
        pwindo.setOutsideTouchable(false);  
Run Code Online (Sandbox Code Playgroud)

第五次尝试:

        pwindo.setOutsideTouchable(true);  
Run Code Online (Sandbox Code Playgroud)

第六次尝试:

        pwindo.setOutsideTouchable(false);  
Run Code Online (Sandbox Code Playgroud)

第7次尝试:

        pwindo.setFocusable(true);  
Run Code Online (Sandbox Code Playgroud)

什么都行不通.

更新:

public void addFlyout()
{
    try {
        LayoutInflater inflater = TabActivity.this.getLayoutInflater();
        Display display = getWindowManager().getDefaultDisplay();
        Method mGetRawH = Display.class.getMethod("getRawHeight");
        Method mGetRawW = Display.class.getMethod("getRawWidth");
        int rawHeight = (Integer) mGetRawH.invoke(display);
        View view = inflater.inflate(R.layout.flyout_list_layout,
                (ViewGroup) findViewById(R.id.flyoutLayoutList));
        Dialog dialog = new Dialog(TabActivity.this);
        pwindo = new PopupWindow(view, 340, rawHeight- actionBar.getHeight(), true);
        pwindo.showAtLocation(view, Gravity.RIGHT | Gravity.TOP, 0, actionBar.getHeight());
        pwindo.setBackgroundDrawable(new ColorDrawable(Color.RED));
       // pwindo.setTouchable(true);
        pwindo.setOutsideTouchable(true);
        pwindo.setTouchInterceptor(new View.OnTouchListener() {
            @Override public boolean onTouch(View v, MotionEvent event)
            {

                if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
                {
                    pwindo.dismiss();
                    //Log.e(TAG, "some event happened outside window: action outside");
                    return true;
                }
               // Log.e(TAG, "some event happened outside window");
                return false;
            }
        });

        listFlyout = (ListView) view.findViewById(R.id.list_slideList);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(TabActivity.this, R.layout.drawer_item, R.id.content, tabs);
        listFlyout.setAdapter(adapter);
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
} 
Run Code Online (Sandbox Code Playgroud)

@Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        try {
            if(tab.getPosition() == 4)
            {
                addFlyout();
            }
            else
            mViewPager.setCurrentItem(tab.getPosition());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Vik*_*ram 5

设置背景PopupWindow确保通过其边界外的单击可以禁止它.

showAtLocation(...):

public void showAtLocation(IBinder token, int gravity, int x, int y) {
    ....

    preparePopup(p);

    ....
}
Run Code Online (Sandbox Code Playgroud)

preparePopup(...)检查是否设置了背景.如果是,则传递的内容视图(在构造函数中)将添加到自定义中FrameLayout.然后在此自定义上设置背景FrameLayout:

if (mBackground != null) {
    ....

    // when a background is available, we embed the content view
    // within another view that owns the background drawable
    PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);
    PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, height
        );
    popupViewContainer.setBackground(mBackground);
    popupViewContainer.addView(mContentView, listParams);

    ....
} else {
    ....
}
Run Code Online (Sandbox Code Playgroud)

由于您正在调用setBackgroundDrawable(...) 之后 showAtLocation(...),preparePopup(...)不会将内容视图放在其中PopupViewContainer.

PopupViewContainer也会覆盖onTouchEvent(...)提供"被解雇时被触及的外部"行为:

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();

    if ((event.getAction() == MotionEvent.ACTION_DOWN)
            && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
        dismiss();
        return true;
    } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
        dismiss();
        return true;
    } else {
        return super.onTouchEvent(event);
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,您需要的只是提供背景,并在致电之前提供showAtLocation(...).