Kailash Dabhi
Kailash Dabhi

Reputation: 3513

popupwindow's event method not fired

When I click on a thumbnail, a popup window opens. I want to dismiss it in the onTouch method but the problem is that the onTouch method is not getting called.So How can i get it run? How can I dismiss the popup window?

my code is:

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

}

Upvotes: 2

Views: 1222

Answers (2)

AlexChu
AlexChu

Reputation: 31

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

    }
});

}

This problem is popupwindow底层的消息机制决定的,因为它是阻塞式的。 This is because the popup window does not respond to onTouch or onKey events unless it has a background that != null. Check out some code I wrote to help with this. In the basic case you can to call PopupWindow#setBackgroundDrawable(new BitmapDrawable()) to force it to act the way you expect. You won't need your own onKey listener. You might also need to call PopupWindow#setOutsideTouchable(true) if you want it to go away when the user clicks outside of the window boundaries.

this is the link ,Good luck!

Upvotes: 0

Parag Chauhan
Parag Chauhan

Reputation: 35966

Try this

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

And onClickEvent

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

Upvotes: 1

Related Questions