在ExpandableListView中自动滚动

jon*_*939 18 android scroll expandablelistview

我希望我ExpandableListView在用户展开组时自动滚动,以便展开的组标题位于屏幕顶部.我试过了smoothScrollToPosition,但这只是确保扩展组在屏幕上的某个位置可见.我想明确滚动它,以便扩展组位于顶部,如下例所示:

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         |
+=================+                      +=================+
Run Code Online (Sandbox Code Playgroud)

Lar*_*man 16

ListView.setSelection(position)
Run Code Online (Sandbox Code Playgroud)

这将滚动到所选项目,当您单击组项目时调用此项目.


小智 6

以下代码是一个适合我的解决方案

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;        
}
Run Code Online (Sandbox Code Playgroud)


the*_*ude 6

这对我有用.把它放在适配器中:

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

    listView.setSelectedGroup(groupPosition);
}
Run Code Online (Sandbox Code Playgroud)