jon4939
jon4939

Reputation: 205

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

Upvotes: 18

Views: 18389

Answers (8)

Mohammed Mukhtar
Mohammed Mukhtar

Reputation: 1741

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;
        }
    });

Upvotes: 0

Ravikant Paudel
Ravikant Paudel

Reputation: 2286

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

Upvotes: 3

Manju
Manju

Reputation: 742

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);
    }
});

}

Upvotes: 0

thecoolmacdude
thecoolmacdude

Reputation: 2277

This worked for me. Put it in your adapter:

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

    listView.setSelectedGroup(groupPosition);
}

Upvotes: 6

Prakash Gavade
Prakash Gavade

Reputation: 61

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

Upvotes: 6

Neonigma
Neonigma

Reputation: 1835

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.

Upvotes: 0

Laura
Laura

Reputation: 2773

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

Upvotes: 2

Lars Werkman
Lars Werkman

Reputation: 2528

ListView.setSelection(position)

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

Upvotes: 18

Related Questions