客戶提出需求,要求分組列表單擊一個group其餘的group應該關閉,這個group的group標題應該頂頭。 java
我網上大體找了一圈沒找到相關的,因而去google group裏搜索了下 ,發現有人提出了他的解決方法: linux
http://groups.google.com/group/Android-developers/browse_thread/thread/638f7ae1cb75b710/0154a631a2f38717?lnk=gst&q=setOnGroupClickListener#0154a631a2f38717 ide
受此啓發我想出了本身的方法,重寫setOnGroupClickListener方法,使用flag控制: google
定義flag記錄列表狀態 spa
int expandFlag = -1;//控制列表的展開 code
重寫onGroupClick方法 get
ExpandableListView listView = getExpandableListView(); listView.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView listView, View v, int groupPosition, long id) { if (expandFlag == -1) { //展開被選的group listView.expandGroup(groupPosition); //設置被選中的group置於頂端 listView.setSelectedGroup(groupPosition); expandFlag = groupPosition; } else if (expandFlag == groupPosition) { listView.collapseGroup(expandFlag); expandFlag = -1; } else { listView.collapseGroup(expandFlag); //展開被選的group listView.expandGroup(groupPosition); //設置被選中的group置於頂端 listView.setSelectedGroup(groupPosition); expandFlag = groupPosition; } return true; } });