How to get the first visible View from an Android ListView
Asked Answered
E

6

10

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.

Eft answered 24/6, 2010 at 14:35 Comment(2)
The views are recycled. What do you need the specific view for?Darnell
I edit my question. I want to change the view after the user scrolled the list.Eft
A
13

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

listView.getChildAt(0)
Arnoldoarnon answered 24/6, 2010 at 15:12 Comment(3)
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.Eft
I think this way is wrong. Because you get first item that loaded. and maybe that item isn't first visible. But listView.getFirstVisiblePosition() is better and work well always.Mlle
Not accurate if there are headers.Moonrise
P
4

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

listView.getChildAt(listView.getFirstVisiblePosition());

Percolation answered 24/6, 2010 at 16:16 Comment(2)
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.Eft
I liked the answer and found it useful to find something working. Yet using childAt with the position in the adapter wsill not work. If you use fetFirstVisiblePosition, you have to pass that along again to the adapter using getItem. So more correct would be adapter.getItem(listview.getFirstVisiblePosition). Yet, in that case you don't have the view as the op asked. Nevertheless, thanks for pointing this out.Flatten
L
4

Indeed listView.getChildAt(listView.getFirstVisiblePosition()) gives the first visible item,
BUT it could be half visible list item.

To get first completely visible list item,

if (listView.getChildAt(0).getTop() < 0) {
     int firstCompletelyVisiblePos = listView.getFirstVisiblePosition() + 1;
}
Lactescent answered 25/2, 2015 at 13:56 Comment(0)
C
1
Object item = listView.getItemAtPosition(listView.getFirstVisiblePosition());

For first completely visible list item:

int pos = listView.getFirstVisiblePosition();
if (listView.getChildCount() > 1 && listView.getChildAt(0).getTop() < 0) pos++;
Object item = listView.getItemAtPosition(pos);
Cordova answered 17/6, 2018 at 9:39 Comment(0)
O
0

You can use the following code:

for (int i = 0; i <= conversationListView.getLastVisiblePosition() - conversationListView.getFirstVisiblePosition(); i++) {
        View listItem = conversationListView.getChildAt(i);
}
Ova answered 21/6, 2016 at 14:4 Comment(0)
V
-2

listView.scrollBy(0, -40);

This works very well

Vickers answered 19/8, 2016 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.