2

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

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

它背后有什么特别的原因还是我在这里做错了什么?

4

1 回答 1

3

来自android来源的评论AbsListView.onSaveInstanceState()

// 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.
于 2013-06-19T16:25:36.097 回答