GetFirstVisiblePosition () returns an invalid value in the gallery window

I have a gallery view created using the SimpleAdapter that extends the BaseAdapter. It contains more items than what can fit on the screen.

Therefore, I use Gallery.getFirstVisiblePosition()both Gallery.getLastVisiblePosition()to get the first and last visible position. But these functions do not always return the correct values, especially after setting the selected position, calling mGallery.setSelection(), and then calling SimpleAdapter.notifyDataChanged(). Most of the time it getFirstVisiblePosition()returns 0, even if the first item is not displayed.

Also note that no. visible elements in the Gallery are different in portrait and landscape modes. The value returned getFirstVisiblePosition()looks correct in the landscape, but returns in portrait mode.

Anyone have any thoughts / ideas?

Thank!

+3
source share
1 answer

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

+3
source

Source: https://habr.com/ru/post/1759927/


All Articles