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:24

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

查看更多
干净又极端
3楼-- · 2019-06-14 23:28

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);
查看更多
登录 后发表回答