Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get index of first/last visible group in an ExpandableListView?

Tags:

android

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.

like image 235
Andreas Avatar asked Aug 29 '10 08:08

Andreas


2 Answers

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++;

    }
}
like image 104
sara Avatar answered Nov 16 '22 18:11

sara


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()...

like image 31
jpm Avatar answered Nov 16 '22 19:11

jpm