我看了How to maintain the position of ListView
该解决方案仅在活动/片段未被销毁时才有效。如果是,就像屏幕旋转一样,我认为你需要将滚动位置放在onSaveInstanceState中的一个包中。否则,当恢复活动/片段时,索引将为0,因为它不仅停止,而且在旋转屏幕后被销毁。
所以我在onSaveInstanceState的片段包中保存了滚动位置,并在onActivityCreated中设置了位置:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("scrollPos", listv.getFirstVisiblePosition());
super.onSaveInstanceState(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
....
int startIndex;
if(savedInstanceState == null)
startIndex = 0;
else
startIndex = savedInstanceState.getInt("scrollPos");
questionsLV.setSelectionFromTop(startIndex, 0);
return v;
}
我在旋转屏幕后看到保存在onActivityCreated中的索引,但我的listview仍然从顶部开始;为什么呢?