How do I get index of first/last visible group in an ExpandableListView?
getFirstVisiblePosition() and getLastVisiblePosition() are mostly useless for ExpandableListViews, because they return the index of the first/last visible cell in the list. Which makes a difference because expanded groups count as multiple cells.
What I do need is either some methods like getFirstVisibleGroupIndex(), getLastVisibleGroupIndex() or some method to convert the "visible cell index" value from the methods above to a real group(+child) index value.
Note: OnScrollListener.onScroll(..., int firstVisibleItem, int visibleItemCount, ...) suffers from the same problem if the listener is set on an ExpandableListView.
Are you looking for something like this?
public void listVisibleRowsForExpandableGroup()
{
int firstVis = getFirstVisiblePosition();
int lastVis = getLastVisiblePosition();
int count = firstVis;
while (count <= lastVis)
{
long longposition = getExpandableListPosition(count);
int type = getPackedPositionType(longposition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPosition = getPackedPositionGroup(longposition);
int childPosition = getPackedPositionChild(longposition);
Log.d("Test","group: " + groupPosition + " and child: " + childPosition );
}
count++;
}
}
I know this Question is old, but for everybody who stumbles upon it as I did... Based on sara's answer this is the method I'm using now:
public int getFirstVisibleGroup() {
int firstVis = list.getFirstVisiblePosition();
long packedPosition = list.getExpandableListPosition(firstVis);
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
return groupPosition;
}
The same should work with getLastVisiblePosition()...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With