【问题标题】:How to save and restore the state of an ExpandableListView in Android?如何在 Android 中保存和恢复 ExpandableListView 的状态?
【发布时间】:2012-05-24 22:44:04
【问题描述】:

是否可以在 Android 中保存和恢复 ExpandableListView 的状态(哪些项目已折叠,哪些未折叠)?

如果可能,我该怎么做?

我可以在 onPause() / onResume() 中访问 ExpandableListView 吗?如何访问?

【问题讨论】:

    标签: android save state expandablelistview restore


    【解决方案1】:

    我遍历组并保存所有组的状态:

    int numberOfGroups = MyExpandableListViewAdapter.getGroupCount();
    boolean[] groupExpandedArray = new boolean[numberOfGroups];
      for (int i=0;i<numberOfGroups;i++)
        groupExpandedArray[i] = MyExpandableListView.isGroupExpanded(i);
    

    然后为恢复状态:

    for (int i=0;i<groupExpandedArray.length;i++)
      if (groupExpandedArray[i] == true)
        MyExpandableListView.expandGroup(i);
    

    我不明白如何在 onPause()/onResume() 中访问 ListView 是什么意思,如果你将 ListView 对象存储为活动类的成员,它应该可以从那里访问。

    【讨论】:

      【解决方案2】:

      改进 Floaf 的答案:声明

       public static int firstVisiblePosition=0;
      

      暂停

      int numberOfGroups = MyExpandableListViewAdapter.getGroupCount();
      boolean[] groupExpandedArray = new boolean[numberOfGroups];
      for (int i=0;i<numberOfGroups;i++){
          groupExpandedArray[i] = MyExpandableListView.isGroupExpanded(i);
      }
      firstVisiblePosition = MyExpandableListView.getFirstVisiblePosition();
      

      onResume

      for (int i=0;i<groupExpandedArray.length;i++){
        if (groupExpandedArray[i] == true)
            MyExpandableListView.expandGroup(i);
      }
      MyExpandableListView.setSelection(firstVisiblePosition );
      

      这不仅会恢复每个组的状态,还会滚动到活动暂停时查看的 childView。

      【讨论】:

        【解决方案3】:

        更简单的方法可能是,如果您使用 setOnGroupClickListner 和 setOnChildClickListener,只需保留最后选择的 ChildPosition 和 GroupPositin 整数,然后检查它并正确响应,

        if (childPosition > 0) {
            mExpandableList.expandGroup(groupPositin);
        }
        mExpandableList.setItemChecked(groupPositin + childPosition , true);
        

        您只需要记住在 setOnGroupListener 方法中将 childPosition 设置为 0。

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题并找到了更好的答案。我有一个片段并使用 onSaveInstanceStateonViewStateRestored 来保存和恢复 ExpandableListView

          这里我保存了列表的状态

          @Override
          public void onSaveInstanceState( @NonNull Bundle outState )
          {
              super.onSaveInstanceState( outState );
          
              ExpandableListView expandable = getView() != null ? getView().findViewById( R.id.expandable ) : null;
              if( expandable != null )
              {
                  int groupsCount = expandable.getExpandableListAdapter()
                                              .getGroupCount();
                  boolean[] groupExpandedArray = new boolean[groupsCount];
                  for( int i = 0; i < groupsCount; i += 1 )
                  {
                      groupExpandedArray[i] = expandable.isGroupExpanded( i );
                  }
                  outState.putBooleanArray( "groupExpandedArray", groupExpandedArray );
                  outState.putInt( "firstVisiblePosition", expandable.getFirstVisiblePosition() );
              }
          }
          

          我会在需要的时候恢复它

          @Override
          public void onViewStateRestored( @Nullable Bundle savedInstanceState )
          {
              super.onViewStateRestored( savedInstanceState );
              if( savedInstanceState != null )
              {
                  boolean[]          groupExpandedArray   = savedInstanceState.getBooleanArray( "groupExpandedArray" );
                  int                firstVisiblePosition = savedInstanceState.getInt( "firstVisiblePosition", -1 );
                  ExpandableListView expandable           = getView() instanceof ViewGroup ? getView().findViewById( R.id.expandable ) : null;
                  if( expandable != null && groupExpandedArray != null )
                  {
                      for( int i = 0; i < groupExpandedArray.length; i++ )
                      {
                          if( groupExpandedArray[i] )
                              expandable.expandGroup( i );
                      }
                      if( firstVisiblePosition >= 0 )
                          expandable.setSelection( firstVisiblePosition );
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-01-22
            • 1970-01-01
            • 2015-10-06
            • 1970-01-01
            • 2013-08-31
            • 2011-07-23
            • 2011-08-28
            • 1970-01-01
            相关资源
            最近更新 更多