tags:

views:

297

answers:

2

I have a gridView that I display in a popupwindow (the gridview is in a transparent layout, which inherits from linearlayout and just has a partially transparent background). I can never get this GridView's OnItemClick to execute. When I touch an image in my gridview it appears to be clicked (image bachgrond changes), but OnItemClick is not being called.

Below is the code for my Adapter and my popupview containing the gridView. Thanks!

//Adapter

public class ImageAdapter extends BaseAdapter { private Context mContext; private int itemBackground;

public ImageAdapter(Context c) {
    mContext = c;

  //---setting the style---
    TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
    itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
    a.recycle();
}

....

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(images[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageView.setBackgroundResource(itemBackground);
    return imageView;

}
public Integer[] images = {
        R.drawable.sound1,
        R.drawable.sound2,
        R.drawable.sound3,
        R.drawable.sound4
};

}

//////////In Activity, onCreate////////

...

final LayoutInflater inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final TransparentLayout musicGrid = (TransparentLayout) inflater.inflate(R.layout.gridviewpopup, null, false);
    final GridView gView = (GridView) musicGrid.findViewById(R.id.music_gridview);
    gView.setAdapter(new ImageAdapter(this));

    final PopupWindow soundSelectorWindow = new PopupWindow(this);
    soundSelectorWindow.setContentView(musicGrid);
    soundSelectorWindow.setTouchable(true);

    gView.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
        {   
            //NEVER GETS HERE
            soundSelectorWindow.dismiss();

        }
    }); 
+2  A: 

what happens if you remove soundSelectorWindow.setTouchable(true);

Alex Volovoy
Same thing as before :/
k_day
I made a very simple app to demonstrate this behavior, in case anyone wants to have a look:http://dl.dropbox.com/u/257677/SamplePopup.zip
k_day
although design of your example is strange, but it's example after all.You don't need to setTouchable - it's true by default. However you do need set popup.setFocusable(true);
Alex Volovoy
Thank you so much Alex! That did the trick. I am still unsure why I was able to scroll through my gridView without the popupwindow focuasable true, but not make a selection.
k_day
+1  A: 

I can't explain why the OnItemClickListener isn't working, but when I substituted with OnTouchListener it worked. Do you need to differentiate between which item is clicked or is it enough just to know that the popup was clicked? From you code it looks like the latter, so OnTouch should work:

    popup.setTouchable(true);
    popup.setFocusable(true);
    gView.setClickable(true);

    gView.setOnTouchListener(new View.OnTouchListener() 
    {
        public boolean onTouch(View v, MotionEvent event) 
        {   
            popup.dismiss();
            return true;
        }
    }); 
RickNotFred
Yeah, that does work too. Unfortunately, I need to know which item was clicked. Thanks for having a look!
k_day