I have an expandable listview. one of my groups, Group1, has 15 children. how can I smooth scroll to the 10th child upon clicking and expanding Group1?
Thanks.
I have an expandable listview. one of my groups, Group1, has 15 children. how can I smooth scroll to the 10th child upon clicking and expanding Group1?
Thanks.
Override your adapter's onGroupExpanded
method and call one the smoothScrollToPosition
methods of your ExpandableListView
. Something like:
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
// If it's the first group
if (groupPosition == 0) {
int scrollTo = groupPosition + 15;
mExpList.smoothScrollToPositionFromTop(scrollTo, 0);
}
}
Now remember that if there are other groups expanded before groupPosition
the scrollTo
will have to take that into account. So you might want to save all the expanded group positions (and their children counts) into an array and loop them every time onGroupExpanded
and onGroupCollapsed
is called.
That might look something like this:
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
groupStates.put(groupPosition, 1);
childrenCount.put(groupPosition, getChildrenCount(groupPosition));
// Loop the group states
int scrollTo = 0;
for (int i=0; i<groupStates.size(); i++) {
int position = groupStates.keyAt(i);
// Cancel the loop if reached a further expanded group
if (position > groupPosition) break;
// 1 is the group to be skipped + its children count
scrollTo += 1 + childrenCount.get(position);
}
// You can select specific child of the recently expanded group, to scroll to
int childToScrollTo = 15; // make sure is an existing child
// -1 because positions begin at index 1
scrollTo = scrollTo - 1 + childToScrollTo;
mExpList.smoothScrollToPositionFromTop(scrollTo, 0);
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
groupStates.remove(groupPosition);
childrenCount.remove(groupPosition);
}
This is however untested so please do make sure you test it properly.