是否可以保存和恢復Android中的ExpandableListView的狀態(哪些項目已摺疊,哪些不是)?如何在Android中保存和恢復ExpandableListView的狀態?
如果可能,我該怎麼做?
我可以訪問onPause()/ onResume()中的ExpandableListView以及如何操作?
是否可以保存和恢復Android中的ExpandableListView的狀態(哪些項目已摺疊,哪些不是)?如何在Android中保存和恢復ExpandableListView的狀態?
如果可能,我該怎麼做?
我可以訪問onPause()/ onResume()中的ExpandableListView以及如何操作?
我通過遍歷組並保存所有的狀態:
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對象作爲活動類的成員存儲,應該可以從那裏訪問它。
一個更簡單的方法可能是,如果你使用setOnGroupClickListner和setOnChildClickListener只保留最後選定ChildPosition和GroupPositin整數,後來檢查,並作出適當的反應,
if (childPosition > 0) {
mExpandableList.expandGroup(groupPositin);
}
mExpandableList.setItemChecked(groupPositin + childPosition , true);
你只需要記住設置childPosition在你的setOnGroupListener方法中爲0。
提高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那在活動暫停時查看。
是Floaf,發現解決方案已經... :) – mrd