views:

55

answers:

1

I have a Gallery view created with a SimpleAdapter that extends BaseAdapter. It contains more items than what it can fit in the screen.

So I'm using Gallery.getFirstVisiblePosition() and Gallery.getLastVisiblePosition() to get the first and last visible position. But these functions doesn't always return the correct values especially after setting the selected position by calling mGallery.setSelection() followed by a SimpleAdapter.notifyDataChanged() call. Most of the time it getFirstVisiblePosition() returns 0 even if the first element is not visible.

Also note that the no. of visible items in Gallery is different in portrait and landscape mode. The value returned by getFirstVisiblePosition() look correct in landscape but is returned wrong in portrait mode.

Anyone has any thoughts/ideas?

Thanks!

A: 

The first visible position will be updated only after a layout. You cannot call notifyDatasetChanged() and right away get the first visible position, you must wait for the next layout pass.

Romain Guy
Thanks for the quick reply Romain! But am not right away getting the first visible position, but am waiting until the next layout pass. Probably I should add some more implementation details. I'm adding photos to the Gallery one by one. Once an image is added, I call the Gallery.setSelection() method so that the last added image is always visible. I call notifydataChanged() in SimpleAdapter after adding the image. So my question is which listener in the Gallery gets called by notifyDataChanged()? It doesn't seem to call onItemSelectedListener and there seems little documentation on this.