English 中文(简体)
如何拯救并恢复Android的扩展式ListView 状态?
原标题:How to save and restore the state of an ExpandableListView in Android?

能否保存并恢复Android 扩展式List View的状态(哪些物品已经倒塌,哪些没有倒塌)?

如果可能的话,我该怎么做呢?

我能访问Pause()/Resume()的扩展式List View吗? 如何访问?

最佳回答

透过各团体,拯救各团体,

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);

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

问题回答

改善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();

续 续 续

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

这将不仅恢复每个组的状态, 还会滚动到活动暂停时所看到的“ 儿童视图 ” 。

更简单的方法可能是, 如果你再使用 SetOn GroupClickListner 和 setOnChildClickLickLister 只保留最后选择的儿童视野和组合Positin整数, 然后再检查它并做出正确回应,

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

您只需在 SetOn GroupListener 方法中记住将儿童位置设为 0 。

我遇到同样的问题,找到了更好的答案。我有一个碎片,用“强势”的“强力”的“拯救Instance State

我在这里保存列表状态

@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 );
        }
    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签