1

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

可能有人知道这是为什么吗?子光标似乎仍然处于活动状态,并且使用覆盖或侦听器强制组保持展开而不管折叠尝试也不起作用(除了阻止用户在展开时更改状态,正如预期的那样) .

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

4

3 回答 3

0

有类似任务的麻烦。ExpandableListview 放置在带有自定义组/项目视图的片段中。我的解决方案是根据这个答案使用 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);
    
于 2013-09-20T11:12:17.760 回答
0

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

onCreateView函数确保展开所有组

mExpandableListView.expandGroup(INCOMPLETE_INDEX, true);
于 2015-06-04T03:55:26.013 回答
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);
    }
}

from:返回 ListView 时保持/保存/恢复滚动位置

于 2015-10-16T11:33:17.533 回答