Save and restore expanded/collapsed state of an ExpandableListActivity

前端 未结 3 1723
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 11:25

I have an ExpandableListActivity (using a SimpleCursorTreeAdapter) which starts another activity when the user clicks on a child-element. When pressing the back-button in th

相关标签:
3条回答
  • 2020-12-29 11:39

    Try this. It will solve the problem, partly

    yourlist.setSaveEnabled(true);
    
    0 讨论(0)
  • 2020-12-29 11:46

    This should work

    Parcelable state = exercisesList.onSaveInstanceState();
    exercisesList.setAdapter(....);
    exercisesList.onRestoreInstanceState(state);
    
    0 讨论(0)
  • 2020-12-29 11:57

    Unfortunately the expanded state always resets whenever the focus is lost and onCreate() is not called when it gets the focus again but onStart(). So my workaround for now is to manually store the IDs of all expanded items and expand them in onStart() again. I implemented a subclass of ExpandableListActivity to reuse the behavior.

    public class PersistentExpandableListActivity extends ExpandableListActivity {
        private long[] expandedIds;
    
        @Override
        protected void onStart() {
            super.onStart();
            if (this.expandedIds != null) {
                restoreExpandedState(expandedIds);
            }
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            expandedIds = getExpandedIds();
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            this.expandedIds = getExpandedIds();
            outState.putLongArray("ExpandedIds", this.expandedIds);
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle state) {
            super.onRestoreInstanceState(state);
            long[] expandedIds = state.getLongArray("ExpandedIds");
            if (expandedIds != null) {
                restoreExpandedState(expandedIds);
            }
        }
    
        private long[] getExpandedIds() {
            ExpandableListView list = getExpandableListView();
            ExpandableListAdapter adapter = getExpandableListAdapter();
            if (adapter != null) {
                int length = adapter.getGroupCount();
                ArrayList<Long> expandedIds = new ArrayList<Long>();
                for(int i=0; i < length; i++) {
                    if(list.isGroupExpanded(i)) {
                        expandedIds.add(adapter.getGroupId(i));
                    }
                }
                return toLongArray(expandedIds);
            } else {
                return null;
            }
        }
    
        private void restoreExpandedState(long[] expandedIds) {
            this.expandedIds = expandedIds;
            if (expandedIds != null) {
                ExpandableListView list = getExpandableListView();
                ExpandableListAdapter adapter = getExpandableListAdapter();
                if (adapter != null) {
                    for (int i=0; i<adapter.getGroupCount(); i++) {
                        long id = adapter.getGroupId(i);
                        if (inArray(expandedIds, id)) list.expandGroup(i);
                    }
                }
            }
        }
    
        private static boolean inArray(long[] array, long element) {
            for (long l : array) {
                if (l == element) {
                    return true;
                }
            }
            return false;
        }
    
        private static long[] toLongArray(List<Long> list)  {
            long[] ret = new long[list.size()];
            int i = 0;
            for (Long e : list)  
                ret[i++] = e.longValue();
            return ret;
        }
    }
    

    Maybe someone has a better solution though.

    0 讨论(0)
提交回复
热议问题