Android: Getting a count of the visible children in a listview Android: Getting a count of the visible children in a listview android android

Android: Getting a count of the visible children in a listview


This is a quick way to get visible children count:

int visibleChildCount = (listView1.getLastVisiblePosition() - listView1.getFirstVisiblePosition()) + 1;


listView.getLastVisiblePosition(), is this what you are looking for? if not, Iteration through child views...

int count = 0;for (int i = 0; i <= listView.getLastVisiblePosition(); i++){    if (listView.getChildAt(i) != null)    {        count++;  // saying that view that counts is the one that is not null,                   // because sometimes you have partially visible items....    }}


In reference to greg7gkb's comment above - just wanted to point out in case anyone is using this that it will make your count off by one. It should be

(listView1.getLastVisiblePosition() - listView1.getFirstVisiblePosition()) + 1

So, if the last visible was 8 and the first visible was 5, you would have (8-5)+1 = 4 showing:5,6,7, and 8.

It looks like A. Abiri got it right below.