How to get index of first / last visible group in ExpandableListView?

How to get index of first / last visible group in ExpandableListView?

getFirstVisiblePosition () and getLastVisiblePosition () are in most cases useless for ExpandableListViews, because they return the index of the first / last visible cell in the list. This matters because extended groups count as multiple cells.

I need either some methods, such as getFirstVisibleGroupIndex (), getLastVisibleGroupIndex (), or some method for converting the index value of visible cells from the above methods into the index value of a real group (+ child).

Note. OnScrollListener.onScroll (..., int firstVisibleItem, int visibleItemCount, ...) suffers from the same problem if the listener is set to ExpandableListView.

+7
android
source share
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++; } } 
+10
source share

I know this question is old, but for everyone who came across it like me ... 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 () ...

+3
source share

All Articles