我希望我的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 |
+=================+ +=================+
答案 0 :(得分:16)
ListView.setSelection(position)
这将滚动到所选项目, 当你点击组项目时调用它。
答案 1 :(得分: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;
}
答案 2 :(得分:6)
这对我有用。把它放在适配器中:
public void onGroupExpanded(final int groupPosition) {
super.onGroupExpanded(groupPosition);
listView.setSelectedGroup(groupPosition);
}
答案 3 :(得分:2)
将此属性android:transcriptMode="disabled"
添加到xml的ExpandibleListView标记中。这应该有用。
答案 4 :(得分: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);
答案 5 :(得分:0)
将android:transcriptMode="disabled"
设置为ExpandibleListView
也适用于我。如果参数设置为“normal”,则无法使用任何方法(setSelectedGroup
,setSelectionFromTop
等)。
只有setSmoothScroll
有效,但不喜欢效果。
答案 6 :(得分: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);
}
});
}
答案 7 :(得分: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;
}
});