Android ExpandableListView如何保存展开/折叠状态?

时间:2016-05-23 12:28:59

标签: android expandablelistview expandablelistadapter

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

getChildView的{​​{1}}内,我只是召唤ParentAdapter

但是,当我展开第3级并将其滚出视图时,它会将自身恢复到折叠状态。

我该如何解决这个问题?

我想,我必须为ChildAdapter中的ChildAdapter保存展开/折叠状态,但我不知道如何正确实现它。

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

1 个答案:

答案 0 :(得分:0)

好吧,看起来,我已经完成了。

首先,在内部添加额外参数isExpanded

ParentAdaptergetChildView添加条件:

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

ChildAdaptergetChildView添加以下内容:

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

这将保存商品的展开/折叠状态并正确更改。

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