Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto scrolling in ExpandableListView

I would like my ExpandableListView to automatically scroll when the user expands a group, so that the expanded group header is at the top of the screen. I've tried smoothScrollToPosition, but this merely ensures the expanded group is visible somewhere on the screen. I would like to explicitly scroll it so the expanded group is at the top, like in this example:

Before expanding Group 3:                After expanding Group 3:

+=================+                      +=================+
| Group 1         |                      | Group 3         |
+-----------------+                      +-----------------+
| Group 2         |                      |   Grp 3 Child 1 |
+-----------------+                      +-----------------+
| Group 3         |                      |   Grp 3 Child 2 |
+-----------------+                      +-----------------+
| Group 4         |                      | Group 4         |
+=================+                      +=================+
like image 834
jon4939 Avatar asked Oct 06 '12 19:10

jon4939


3 Answers

ListView.setSelection(position)

this will scroll to the selected item, call this when u click on the group item.

like image 76
Lars Werkman Avatar answered Oct 31 '22 11:10

Lars Werkman


The following code is a solution that worked for me

public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {
    // TODO Auto-generated method stub
    //mExpandableList.setSelectionFromTop(groupPosition, 0);

Boolean shouldExpand = (!mExpandableList.isGroupExpanded(groupPosition));        
    mExpandableList.collapseGroup(lastClickedPosition);

    if (shouldExpand){
        //generateExpandableList();
        mExpandableList.expandGroup(groupPosition);
        mExpandableList.setSelectionFromTop(groupPosition, 0);
    }                
    lastClickedPosition = groupPosition;
    return true;        
}
like image 6
Prakash Gavade Avatar answered Oct 31 '22 10:10

Prakash Gavade


This worked for me. Put it in your adapter:

public void onGroupExpanded(final int groupPosition) {
    super.onGroupExpanded(groupPosition);

    listView.setSelectedGroup(groupPosition);
}
like image 6
thecoolmacdude Avatar answered Oct 31 '22 09:10

thecoolmacdude