18

我希望我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         |
+=================+                      +=================+
4

8 回答 8

18
ListView.setSelection(position)

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

于 2012-10-06T19:25:55.603 回答
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;        
}
于 2013-03-20T12:04:56.983 回答
6

这对我有用。把它放在你的适配器中:

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

    listView.setSelectedGroup(groupPosition);
}
于 2014-07-17T18:33:10.750 回答
2

将此属性添加android:transcriptMode="disabled"到 xml 中的 ExpandibleListView 标记。这应该有效。

于 2012-10-06T20:18:33.980 回答
2

这个对我有用

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

由于滚动的主要工作部分是

parent.setSelectedGroup(groupPosition);
于 2015-09-23T06:25:05.220 回答
0

设置android:transcriptMode="disabled"为我ExpandibleListView也为我工作。将参数设置为“正常”时,没有一种方法有效(setSelectedGroup、、setSelectionFromTop等)。

只能setSmoothScroll工作,但不喜欢这种效果。

于 2013-03-04T16:20:28.997 回答
0

下面的代码对我有用。希望它会有所帮助。在 onGroupExpand 中实现 OnGroupExpandListener 使用下面的代码

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

}

于 2015-03-24T12:19:50.427 回答
0

setSelectedGroup工作,但如果你想有一个平滑的滚动效果,使用smoothScrollToPositionFromTop如下:

    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;
        }
    });
于 2017-03-27T14:37:21.420 回答