【问题标题】:Android ExpandableListView how to save expanded/collapsed state?Android ExpandableListView 如何保存展开/折叠状态?
【发布时间】:2016-05-23 12:28:59
【问题描述】:

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

ParentAdaptergetChildView 里面我只是召唤ChildAdapter

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

我该如何解决这个问题?

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

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

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:

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

    首先,在你的项目中添加额外的参数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();
       }
    

    这将保存您的项目的展开/折叠状态并正确更改它们。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 2011-06-23
      • 1970-01-01
      • 2017-04-09
      • 2015-12-25
      相关资源
      最近更新 更多