1

I have a standard ExpandableListView backed by a custom CursorTreeAdapter and custom layouts. It works fine except for the fact that if I rotate the screen twice (i.e. portrait to landscape then back to portrait, or landscape to portrait then back to landscape) then all the groups appear to have collapsed, and they cannot be re-expanded. Further rotations change nothing.

Might anybody know why this is? The child cursors still appear to be active, and using overrides or listeners to force the groups to stay expanded regardless of collapse attempts doesn't work either (except to block the user from changing the state when it is expanded, as would be expected).

I can update my post to provide my code or layouts if anybody requires.

3 Answers3

0

Having trouble with similar task. ExpandableListview placed in Fragment with custom group/item views. My solution is use SharedPreferences based on this answer. It's may be not optimal (I learn Java/Android less than a month) but only one that worked. Storing/restoring via Bundle does not work for me :(

  1. Write 2 methods for collecting and setting expanded states:

    private ArrayList<Long> getExpandedIds() {
      // private ExpandableListView lv defined above
      BaseExpandableListAdapter adp = (BaseExpandableList) lv.getExpandableListAdapter();
    
      if (adp != null) {
        int sz = adp.getGroupCount();
        ArrayList<Long> ids = new ArrayList<Long>();
        for (int i=0; i<sz; i++) 
            if lv.isGroupExpanded(i)) ids.add(adp.getGroupId(i));
        return ids;
      } else {
        return null;
      }
    }
    
    private void setExpandedIds(ArrayList<Long> ids) {
    
        if ((ids == null) || (ids.size()==0)) return;
    
        BaseExpandableListAdapter adp = (BaseExpandableListAdapter) lv.getExpandableListAdapter();
        if (adp != null) {
            int sz = adp.getGroupCount();
            for (int i=0;i<sz;i++) {
                long id = adp.getGroupId(i);
                for (long n : ids) {
                    if (n == id) {
                        lv.expandGroup(i);
                        break;
                    }
                }
            }
        }
    }
    
  2. Use above methods for save and restore state in SharedPreferences:

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        ArrayList<Long> expIds = getExpandedIds();
        if (expIds != null) {
            try {
                @SuppressWarnings("ConstantConditions")
                SharedPreferences pref = this.getActivity().getPreferences(Activity.MODE_PRIVATE);
                SharedPreferences.Editor ed = pref.edit();
                String s = TextUtils.join(";", expIds.toArray());
                ed.putString(LV_NAME, s);
                ed.commit();
            } catch (NullPointerException e) {
                Log.e("YAMK", "Store prefs failed.");
            }
        }
    }
    
    // in onCreateView:
    try {
        @SuppressWarnings("ConstantConditions")
        SharedPreferences pref = getActivity().getPreferences(Activity.MODE_PRIVATE);
        ArrayList<Long>expIds = new ArrayList<Long>();
        for (String s : TextUtils.split(pref.getString(LV_NAME, ""), ";")) {
            expIds.add(Long.parseLong(s));
        }
    } catch (NullPointerException e) {
        //
    }
    setExpandedIds(expIds);
    
Community
  • 1
  • 1
A.V
  • 41
  • 5
0

After 3 days searching and try many solutions. I found the root cause it's very simple.

on onCreateView function make sure expand all groups

mExpandableListView.expandGroup(INCOMPLETE_INDEX, true);
Tai Le Anh
  • 268
  • 3
  • 10
0

Try this (maintain the list position too):

Parcelable state;

@Override
public void onPause() {    
    // Save ListView state @ onPause
    Log.d(TAG, "saving listview state @ onPause");
    state = listView.onSaveInstanceState();
    super.onPause();
}
...

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Set new items
    listView.setAdapter(adapter);
    ...
    // Restore previous state (including selected item index and scroll position)
    if(state != null) {
        Log.d(TAG, "trying to restore listview state..");
        listView.onRestoreInstanceState(state);
    }
}

from: Maintain/Save/Restore scroll position when returning to a ListView

Community
  • 1
  • 1
miguel_rossi
  • 53
  • 2
  • 7