AndroidExpandableListView如何保存expanded/collapsed状态?

Android ExpandableListView how to save expanded/collapsed state?

我有一个自定义的 3 级可扩展列表视图。使用 2 个适配器完成,扩展 BaseExpandableListAdapterParentAdapterChildAdapter.

ParentAdaptergetChildView 里面 我刚刚召唤 ChildAdapter.

但是当我展开第 3 层并将其滚动到视图之外时,它会自行恢复到折叠状态。

我该如何解决这个问题?

我想,我必须在 ChildAdapter 中为我的 getGroupView 保存 expanded/collapsed 状态,但我不知道如何正确实现它。

或者我应该在 ParentAdapter 中保存 getChildView 的状态吗?

好的,看来我做到了

首先,在你的项目中添加额外的参数isExpanded

ParentAdapter里面getChildView添加条件:

  //expand child if was previously expanded
  if (item.isExpanded)
  {
    expandableListView.expandGroup(childPosition);
  } 

ChildAdapter 里面 getChildView 添加:

    if (item.isExpanded)
   {
     expandableListView.expandGroup(groupPosition);
     notifyDataSetChanged();
   } else {
     expandableListView.collapseGroup(groupPosition);
     notifyDataSetChanged();
   }

这将保存您的项目的 expanded/collapsed 状态并正确更改它们。

至少,这个实现解决了我的问题,尽管可能存在更好的解决方案。