popupwindow的事件方法没有被触发

时间:2012-09-04 06:26:19

标签: android

当我点击缩略图时,会打开一个弹出窗口。我想在onTouch方法中忽略它,但问题是onTouch方法没有被调用。所以如何让它运行?如何解除弹出窗口?

我的代码是:

public void onItemImageClicked(View view)
{
    LinearLayout layout = (LinearLayout)     getLayoutInflater().inflate(R.layout.popup,null); 
    ImageView fullSizeImg = (ImageView) layout.findViewById(R.id.fullSizeImage);
    fullSizeImg.setImageBitmap(bitmap);
    fullSizeImg.setFocusable(false);
    Display display = getWindowManager().getDefaultDisplay(); 
    int deviceWidth = display.getWidth()-40; 
    int deviceHeight = display.getHeight()-70; 
    popupWindow = new PopupWindow(layout, deviceWidth, deviceHeight, true);
    // display the popup in the center
    popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
    popupWindow.getMaxAvailableHeight(new View(this));
    popupWindow.setFocusable(true);
    popupWindow.setTouchInterceptor(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
            return false;
        }
    });
    popupWindow.setTouchable(true);

}

2 个答案:

答案 0 :(得分:1)

试试这个

                View popupView = globalconstant.layoutInflater.inflate(R.layout.popup, null);  
                popupWindow = new PopupWindow(popupView,globalconstant.displayWidth-20,480,true);
                popupWindow.setOutsideTouchable(true);
                popupWindow.setBackgroundDrawable(new BitmapDrawable());

onClickEvent

popupWindow.showAtLocation(v, Gravity.CENTER, 0, 15);

答案 1 :(得分:0)

private void initPopupWindow() {  
// TODO Auto-generated method stub  

View view = getLayoutInflater().inflate(R.layout.main_choice, null);  

ListView main_menu_listview = (ListView) view.findViewById(R.id.main_menu_listview);  

ShowMainChoice madapter = new ShowMainChoice(context);
main_menu_listview.setAdapter(madapter);

int width = (int)getWindowManager().getDefaultDisplay().getWidth()/2;
popupWindow = new PopupWindow(view, width,WindowManager.LayoutParams.WRAP_CONTENT);  
popupWindow.setBackgroundDrawable(new BitmapDrawable());//this is important,如果缺少这句将导致其他任何控件及监听都得不到响应
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);

main_menu_listview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
        // TODO Auto-generated method stub

        Log.e("++++++>", arg2+"");

    }
});

}

  

这个问题是popupwindow底层的消息机制决定的,因为它是阻塞式的。   这是因为弹出窗口不响应onTouch或onKey事件,除非它的背景为!= null。看看我写的一些代码来帮助解决这个问题。在基本情况下,您可以调用PopupWindow #setBackgroundDrawable(new BitmapDrawable())来强制它按照您期望的方式运行。您不需要自己的onKey侦听器。您可能还需要调用PopupWindow#setOutsideTouchable(true),如果您希望它在用户点击窗口边界外时消失。

这是link,祝你好运!