I am developing a tabbed application in which one of the fragments, CollectionsFragment, contains a GridView with an ImageView in each slot. I would like the to use a selec
I think you are mistaken about setDrawSelectorOnTop(boolean)
. The selector
drawable that is being referenced here is GridView's internal selector
drawable.
Even in the simplest implementation of GridView
, when a grid item is clicked, the blue border is drawn around it. This is because, by default, gridview's own selector is drawn behind
the item. If you call setDrawSelectorOnTop(true)
, the selector (blue) will be drawn over the item.
But setDrawSelectorOnTop(boolean)
has nothing to do with the selector you are setting in the adapter. Whether you pass true
, or false
, the ImageView's selector's behavior won't change.
Solution:
Instead of setting the selector on each ImageView inside the adapter, make the GridView use your selector drawable:
GridView gridView = (GridView)view.findViewById(R.id.gridview);
gridView.setDrawSelectorOnTop(true);
// Make GridView use your custom selector drawable
gridView.setSelector(getResources().getDrawable(R.drawable.selector));
Now, there's no need for:
picture.setBackgroundResource(R.drawable.selector);
Edit:
Although I don't recommend this (obvious overhead), it should work:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
....
....
LayerDrawable ld = new LayerDrawable(new Drawable[]
// Drawable from item
{ getResources().getDrawable(item.drawableId),
// Selector
getResources().getDrawable(R.drawable.selector)});
// Set the LayerDrawable
picture.setImageDrawable(ld);
// Don't need this
// picture.setBackgroundResource(R.drawable.selector);
return v;
}
Try setting the xml attribute android:drawSelectorOnTop
in your activity_collections.xml
file
See if placing gridView.setDrawSelectorOnTop(true);
after gridView.setAdapter();
helps. Sometimes, the order matters (weird)
If all else fails, you may have to switch GridView to some other view where setDrawSelectorOnTop() is proven to work consistently.
HTH