Auto scrolling in ExpandableListView

2019-06-14 22:35发布

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

8条回答
虎瘦雄心在
2楼-- · 2019-06-14 23:08

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;        
}
查看更多
再贱就再见
3楼-- · 2019-06-14 23:12
ListView.setSelection(position)

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

查看更多
祖国的老花朵
4楼-- · 2019-06-14 23:15

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.

查看更多
混吃等死
5楼-- · 2019-06-14 23:17

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;
        }
    });
查看更多
叼着烟拽天下
6楼-- · 2019-06-14 23:20

This worked for me. Put it in your adapter:

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

    listView.setSelectedGroup(groupPosition);
}
查看更多
叼着烟拽天下
7楼-- · 2019-06-14 23:22

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

}

查看更多
登录 后发表回答