在可展开的listview android中折叠除选定组以外的所有组
我正在开发Android应用程序使用可扩展列表视图。 其实我需要的是,我是上市组,其中包含孩子。
如果我select一个不可扩展的组,应该展开,当我select第二个组之后,第一个组应该被折叠。 我做了谷歌,但我找不到我想要的。 请帮我一下
将当前扩展的组位置存储在一个variables中。 在onGroupExpanded中执行以下操作。
private int lastExpandedPosition = -1; private ExpandableListView lv; //your expandable listview ... lv.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) { lv.collapseGroup(lastExpandedPosition); } lastExpandedPosition = groupPosition; } });
使用这个代码,这将工作
expandableList.setOnGroupExpandListener(new OnGroupExpandListener() { int previousItem = -1; @Override public void onGroupExpand(int groupPosition) { if(groupPosition != previousItem ) expandableList.collapseGroup(previousItem ); previousItem = groupPosition; } });
@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; }
获取ExpendableListView
,然后重写以下方法 – setOnGroupExpandListener
expandableListView = (ExpandableListView) findViewById(R.id.exp_listview); expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { int previousItem = -1; @Override public void onGroupExpand(int groupPosition) { if (groupPosition != previousItem) expandableListView.collapseGroup(previousItem); previousItem = groupPosition; } });
elstListView1.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { for(int i=0;i<listDataHeader.size();i++){ if(i==groupPosition){ //do nothing} else{ elstListView1.collapseGroup(i); } } } });
尝试把这个放在你的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; }
如果ExpandableListView有两个以上的组:
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { for (int g = 0; g < expandableListAdapter.getGroupCount(); g++) { if (g != groupPosition) { expandableListView.collapseGroup(g); } } } });
除了点击之外,它将会崩溃所有组。
我也面临同样的问题。 但我可以通过以下代码解决我的问题:
正如我在我的可扩展列表中有4个头,根据我写的这样
expListView.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int groupPos) { // TODO Auto-generated method stub expListView.collapseGroup(groupPos+1); expListView.collapseGroup(groupPos-1); expListView.collapseGroup(groupPos-2); expListView.collapseGroup(groupPos+2); expListView.collapseGroup(groupPos+3); expListView.collapseGroup(groupPos-3); } });`