views:

90

answers:

2

Is there a way to get the first visible View out of the ListView in Android?

I can get the data that backs the first View in the Adapter but it seems I can't get the first View in ListView.

I want to change the first visible view after a scroll action finished. I know that I should not save references to the view.

+1  A: 

Actually ListView items are just children of ListView. So first visible ListView item is:

listView.getChildAt(0)
Fedor
thank you works great if you probe the returned view for null. It seems that scrolling and list building sometimes happen at the same time if you start the activity and sometimes the list does not have a child.
Janusz
A: 

ListView has a function getFirstVisiblePosition so to get the first visible view, the code would be:

listView.getChildAt(listView.getFirstVisiblePosition());

Mr. Ambiguous
There is a difference between the childAt() position and the position that is returned by getFirstVisiblePosition(). The position of getFirstVisiblePosition is the position in my data adapter. ChildAt only takes the views into account that are visible in the listview at the moment.
Janusz