2012-05-24 150 views

回答

4

我通過遍歷組並保存所有的狀態:

int numberOfGroups = MyExpandableListViewAdapter.getGroupCount(); 
boolean[] groupExpandedArray = new boolean[numberOfGroups]; 
    for (int i=0;i<numberOfGroups;i++) 
    groupExpandedArray[i] = MyExpandableListView.isGroupExpanded(i); 

那麼對於恢復狀態:

for (int i=0;i<groupExpandedArray.length;i++) 
    if (groupExpandedArray[i] == true) 
    MyExpandableListView.expandGroup(i); 

我不明白您如何訪問是什麼意思在onPause()/ onResume()中的ListView,如果將ListView對象作爲活動類的成員存儲,應該可以從那裏訪問它。

+0

是Floaf,發現解決方案已經... :) – mrd

0

一個更簡單的方法可能是,如果你使用setOnGroupClickListner和setOnChildClickListener只保留最後選定ChildPosition和GroupPositin整數,後來檢查,並作出適當的反應,

if (childPosition > 0) { 
    mExpandableList.expandGroup(groupPositin); 
} 
mExpandableList.setItemChecked(groupPositin + childPosition , true); 

你只需要記住設置childPosition在你的setOnGroupListener方法中爲0。

3

提高Floaf的回答是:在暫停申報

public static int firstVisiblePosition=0; 

int numberOfGroups = MyExpandableListViewAdapter.getGroupCount(); 
boolean[] groupExpandedArray = new boolean[numberOfGroups]; 
for (int i=0;i<numberOfGroups;i++){ 
    groupExpandedArray[i] = MyExpandableListView.isGroupExpanded(i); 
} 
firstVisiblePosition = MyExpandableListView.getFirstVisiblePosition(); 

的onResume

for (int i=0;i<groupExpandedArray.length;i++){ 
    if (groupExpandedArray[i] == true) 
     MyExpandableListView.expandGroup(i); 
} 
MyExpandableListView.setSelection(firstVisiblePosition); 

這不僅會恢復每個組的狀態,但它也將滾動到childView那在活動暫停時查看。