2012-09-04 87 views
2

当我单击缩略图时,会打开一个弹出窗口。我想在onTouch方法中解雇它,但问题是onTouch方法没有被调用。所以我怎样才能让它运行?我怎样才能关闭弹出窗口?popupwindow的事件方法未被触发

我的代码是:

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); 

} 
+0

添加此行popupWindow.setBackgroundDrawable(new BitmapDrawable());并检查它会工作。 –

+0

我已经添加过,但它不起作用。 –

+0

加另一个\t popupWindow.setOutsideTouchable(true); –

回答

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); 
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底层的消息机制决定的,因为它是阻塞式的。看看我写的一些代码来帮助解决这个问题。在基本情况下,您可以调用PopupWindow#setBackgroundDrawable(new BitmapDrawable())来强制它按照您的预期行事。你不需要你自己的onKey监听器。如果您希望在用户单击窗口边界之外时单击它,您可能还需要调用PopupWindow#setOutsideTouchable(true)。

这是link,祝你好运!