Auto scrolling in ExpandableListView
Asked Answered
F

8

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         |
+=================+                      +=================+
Fouquet answered 6/10, 2012 at 19:10 Comment(0)
U
18
ListView.setSelection(position)

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

Uncinate answered 6/10, 2012 at 19:25 Comment(2)
It it possible to achive the same but with the smooth scrolling anuimation?Michey
Maybe this could help you to achieve smoothscrolling https://mcmap.net/q/670042/-wait-unitl-listview-39-s-smoothscrolltoposition-finishes (but you probably need to calculate the height for every item on the list)Uncinate
E
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;        
}
Embarrass answered 20/3, 2013 at 12:4 Comment(0)
P
6

This worked for me. Put it in your adapter:

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

    listView.setSelectedGroup(groupPosition);
}
Peepul answered 17/7, 2014 at 18:33 Comment(0)
F
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);
Fleshy answered 23/9, 2015 at 6:25 Comment(1)
If you use parent.expandGroup(groupPosition, true) then the animation will happen automatically and you won't have to do it yourself.Periapt
E
2

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

Eulaliaeulaliah answered 6/10, 2012 at 20:18 Comment(0)
O
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.

Ophthalmic answered 4/3, 2013 at 16:20 Comment(0)
H
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);
    }
});

}

Harass answered 24/3, 2015 at 12:19 Comment(0)
A
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;
        }
    });
Aculeus answered 27/3, 2017 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.