如果未滚动ListView,则ListView.onSaveInstanceState()不起作用

时间:2013-06-19 15:51:06

标签: android android-listview

我最近遇到了保存ListView位置的问题。我正在使用Parcelable state = ListView.onSaveInstanceState()来存储ListView的状态。并在需要时使用fileView.onRestoreInstanceState(state)恢复位置。

但只有在ListView中滚动了至少一个项目时,它才会将值存储在state中。因此,如果用户不滚动ListView并使用ListView.onSaveInstanceState(),则会在fileView.onRestoreInstanceState(state)上恢复任何内容,并且旧位置仍保留在屏幕上。

背后是否有任何特殊原因或我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

来自AbsListView.onSaveInstanceState()的{​​{3}}的评论:

// Remember the position of the first child.
// We only do this if we are not currently at the top of
// the list, for two reasons:
// (1) The list may be in the process of becoming empty, in
// which case mItemCount may not be 0, but if we try to
// ask for any information about position 0 we will crash.
// (2) Being "at the top" seems like a special case, anyway,
// and the user wouldn't expect to end up somewhere else when
// they revisit the list even if its content has changed.