ExpandableListView组在旋转后卡住了折叠状态

时间:2013-07-30 16:53:19

标签: android expandablelistview screen-orientation

我有一个由自定义ExpandableListView和自定义布局支持的标准CursorTreeAdapter。它工作得很好,除了如果我旋转屏幕两次(即纵向到横向然后回到纵向,或横向到纵向然后回到横向),然后所有组似乎都已折叠,并且它们无法重新展开。进一步的轮换没有任何改变。

有人知道为什么会这样吗?子游标仍然显示为活动状态,并且使用覆盖或侦听器强制组保持扩展而不管崩溃尝试也不起作用(除非阻止用户在展开时更改状态,如预期的那样)

如果有人要求,我可以更新我的帖子以提供我的代码或布局。

3 个答案:

答案 0 :(得分:0)

遇到类似任务的麻烦。 ExpandableListview放置在Fragment中,带有自定义组/项视图。 我的解决方案是使用基于this answer的SharedPreferences。它可能不是最优的(我学习Java / Android不到一个月),但只有一个有效。通过Bundle存储/恢复对我不起作用:(

  1. 编写2种收集和设置扩展状态的方法:

    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. 在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);
    

答案 1 :(得分:0)

经过3天的搜索并尝试了许多解决方案。我发现根本原因很简单。

onCreateView 功能

确保展开所有群组

mExpandableListView.expandGroup(INCOMPLETE_INDEX, true);

答案 2 :(得分:0)

试试这个(保持列表位置):

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);
    }
}

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

相关问题