18

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         |
+=================+                      +=================+
jon4939
  • 205
  • 1
  • 2
  • 5

8 Answers8

18
ListView.setSelection(position)

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

Lars Werkman
  • 2,528
  • 2
  • 20
  • 20
  • It it possible to achive the same but with the smooth scrolling anuimation? – 7heViking Jul 18 '14 at 16:22
  • Maybe this could help you to achieve smoothscrolling http://stackoverflow.com/a/7929080/839632 (but you probably need to calculate the height for every item on the list) – Lars Werkman Aug 19 '14 at 13:16
6

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;        
}
Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
6

This worked for me. Put it in your adapter:

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

    listView.setSelectedGroup(groupPosition);
}
thecoolmacdude
  • 2,036
  • 1
  • 23
  • 38
3

This one is working for me

expandList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

            if (!parent.isGroupExpanded(groupPosition)) {
                parent.expandGroup(groupPosition);
            } else {
                parent.collapseGroup(groupPosition);
            }
            parent.setSelectedGroup(groupPosition);

            return true;
        }
    });

As the main working part for scroll is

parent.setSelectedGroup(groupPosition);
Ravikant Paudel
  • 2,228
  • 2
  • 17
  • 28
  • 1
    If you use `parent.expandGroup(groupPosition, true)` then the animation will happen automatically and you won't have to do it yourself. – ben May 25 '16 at 13:25
2

Add this attribute android:transcriptMode="disabled" to your ExpandibleListView tag from xml. This should work.

Laura
  • 2,653
  • 7
  • 37
  • 59
0

Setting android:transcriptMode="disabled"to my ExpandibleListView worked for me too. With the parameter set to "normal", no one method works (setSelectedGroup, setSelectionFromTop, etc).

Only setSmoothScroll works, but don't like the effect.

Neonigma
  • 1,825
  • 17
  • 20
0

The below code works for me.Hope it will helps.Implements the OnGroupExpandListener within onGroupExpand use the below code

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

expandableListView.post(new Runnable() {

    @Override
    public void run() {
        expandableListView.setSelection(groupPosition);
        if(expandableListView.getChildAt(groupPosition)!=null)
        expandableListView.requestChildRectangleOnScreen(expandableListView.getChildAt(groupPosition),
                new Rect(0, 0, expandableListView.getChildAt(groupPosition).getRight(), expandableListView.getChildAt(groupPosition).getHeight()), false);
    }
});

}

Manju
  • 720
  • 9
  • 23
0

setSelectedGroup works, but if you want to have a smooth scrolling effect, use smoothScrollToPositionFromTop as given below:

    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            parent.smoothScrollToPositionFromTop(groupPosition,0);
            if (expandableListView.isGroupExpanded(groupPosition))
                expandableListView.collapseGroupWithAnimation(groupPosition);
            else expandableListView.expandGroupWithAnimation(groupPosition);
            return true;
        }
    });
Mohammed Mukhtar
  • 1,731
  • 20
  • 18