可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 |
+=================+ +=================+
回答1:
ListView.setSelection(position)
this will scroll to the selected item,
call this when u click on the group item.
回答2:
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:
This worked for me. Put it in your adapter:
public void onGroupExpanded(final int groupPosition) {
super.onGroupExpanded(groupPosition);
listView.setSelectedGroup(groupPosition);
}
回答4:
Add this attribute android:transcriptMode="disabled"
to your ExpandibleListView tag from xml. This should work.
回答5:
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);
回答6:
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.
回答7:
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);
}
});
}
回答8:
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;
}
});