从底部堆叠时如何保存/恢复列表视图状态?

时间:2014-09-09 14:15:47

标签: android android-listview

我有一个ListView和两种情况:

setStackFromBottom(false)

1)当我到达底部时,我使用Parceable currentScrollState = lv.onSaveInstanceState();来保存ListView的状态。然后加载了一些更多的数据,Adapter的数据集发生了变化,我使用lv.onRestoreInstanceState(currentScrollState);来保持滚动状态等于其先前的状态。这很好。

2)然而,当我使用setStackFromBottom(true)时,这不起作用,而ListView总是在适配器数据集更改后滚动到其下边缘。

我试图记录它的状态,发现第一种情况保存得正确:

   AbsListView.SavedState{4260fed0 selectedId=-9223372036854775808 firstId=10 viewTop=-112 position=10 height=1497 filter=null checkState=null}
   AbsListView.SavedState{4263cad8 selectedId=-9223372036854775808 firstId=30 viewTop=-114 position=30 height=1497 filter=null checkState=null}
   AbsListView.SavedState{428ba9e0 selectedId=-9223372036854775808 firstId=50 viewTop=-102 position=50 height=1497 filter=null checkState=null}

使用setStackFromBottom(true)时,ListView无法正确保存状态:

  AbsListView.SavedState{4262c808 selectedId=-9223372036854775808 firstId=-1 viewTop=0 position=0 height=1497 filter=null checkState=null}
  AbsListView.SavedState{428ac3b8 selectedId=-9223372036854775808 firstId=-1 viewTop=0 position=0 height=1497 filter=null checkState=null}

问题:如何在使用ListView时保存/恢复setStackFromBottom(true)的滚动状态?

1 个答案:

答案 0 :(得分:1)

尝试ListView:getFirstVisiblePosition()并使用ListView:setSelection()恢复。

相关问题