I have a gallery of images, in which I put a red border around the image that is currently selected (when the user first opens the screen, the first one is selected, so I draw the border around the first view).
When I click another item, I want to draw the red border in the new item and erase the red border of the previous item.
The problem is that I can't change the border inside the onClick
event of the gallery. If I change it, nothing happens (I think because the items of the gallery do not get redraw). I have tried to called invalidate() and postInvalidate()
in both my gallery variable and in each one of its views, but it doesn`t work! The views do not get redraw (that is, the red border remains only in the first item of the gallery).
Does anybody have any ideas what is going on here?
In your adapter code, hold an int
value that will be the position of the selected View
. In the onItemClicked
method for the Gallery
, update the int
variable with the supplied position. Then, in the getView
method, if the position being drawn is the selected position, draw your border. You can go ahead and apply your border with the supplied View
in onItemClicked
, or call notifyDataSetChanged
on the adapter. The first method is perfered, as notifyDataSetChanged
will recreate every visible View
.
Normally, you would also need to check to see if it's not the selected position so you can remove the border from a reused View
. However, the Gallery
always creates a new View
. convertView
is always null for a Gallery
.