以编程方式折叠ExpandableListView中的组
当我扩大一个新的组,我可以折叠最后一个扩大?
尝试把这个放在你的ExpandableListAdapter
, listView
是对ExpandableListView
本身的引用。 lastExpandedGroupPosition
是在ExpandableListAdapter
定义的整数成员variables。
@Override public void onGroupExpanded(int groupPosition){ //collapse the old expanded group, if not the same //as new group to expand if(groupPosition != lastExpandedGroupPosition){ listView.collapseGroup(lastExpandedGroupPosition); } super.onGroupExpanded(groupPosition); lastExpandedGroupPosition = groupPosition; }
非常有帮助,但正如Anh Tuan在上面的评论中提到的那样,我遇到了ExpandableListView的问题,然后滚动回当前选定的组的顶部(它将停留在当前滚动的位置,在组的中间) 。 您还需要添加一个onGroupClickListener()滚动到正确的位置:
@Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { // Implement this method to scroll to the correct position as this doesn't // happen automatically if we override onGroupExpand() as above parent.smoothScrollToPosition(groupPosition); // Need default behaviour here otherwise group does not get expanded/collapsed // on click if (parent.isGroupExpanded(groupPosition)) { parent.collapseGroup(groupPosition); } else { parent.expandGroup(groupPosition); } return true; }
这对我有效
expandableList.setOnGroupExpandListener(new OnGroupExpandListener() { int previousItem = -1; @Override public void onGroupExpand(int groupPosition) { if(groupPosition != previousItem ) expandableList.collapseGroup(previousItem ); previousItem = groupPosition; } });
做到这一点,扩大点击组,并折叠所有其他人
public void onGroupExpand(int groupPosition) { for (int i = 0; i < len; i++) { if (i != groupPosition) { expandableListDetailsLevel.collapseGroup(i); } } }
它为我工作。
@Override public void onGroupExpanded(int groupPosition) { for(int i=0;i<mGroupCollection.size();i++){ if(i==groupPosition){ System.out.println("Nothing"); } else{ mExpandableListView.collapseGroup(i); } } super.onGroupExpanded(groupPosition); }