滑动ViewPager时Fragment不调用onSaveInstanceState?
我的 ViewPager 遇到问题,我的 ListView 失去了滚动位置。
ListView 的状态可以使用以下方法轻松存储和恢复:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.frag_cool_things, container, false);
AdvListView listView = (AdvListView) v.findViewById(R.id.lv0);
listView.setOnItemClickListener( mOnListItemClicked );
if (null != savedInstanceState)
{
listView.onRestoreListViewInstanceState(savedInstanceState.getParcelable("list_state"));
}
mListView = listView;
return v;
}
@Override
public void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putParcelable("list_state", mListView.onSaveListViewInstanceState());
}
但是问题是,当片段被滑动时,onDestroyView()
被调用,但从未调用 onSaveInstanceState (Bundle outState)
。
旋转屏幕等可以很好地恢复 ListView 状态,但滑动我无法弄清楚如何正确恢复列表。
I'm having an issue with the ViewPager where my ListView is loosing it's scroll position.
The state of the ListView can easily be stored and restored using:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.frag_cool_things, container, false);
AdvListView listView = (AdvListView) v.findViewById(R.id.lv0);
listView.setOnItemClickListener( mOnListItemClicked );
if (null != savedInstanceState)
{
listView.onRestoreListViewInstanceState(savedInstanceState.getParcelable("list_state"));
}
mListView = listView;
return v;
}
@Override
public void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putParcelable("list_state", mListView.onSaveListViewInstanceState());
}
However the problem is that when fragments are being swiped onDestroyView()
gets called but never calls onSaveInstanceState (Bundle outState)
.
Rotating the screen and such restores the ListView
state just fine but swiping I can't figure out how to restore the list properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
2011 年 12 月 17 日更新:
我实际上找到了保存片段内容的正确方法。您必须使用FragmentStatePagerAdapter。该适配器正确保存了片段的状态! :)
旧:
好吧,我找到了一种方法来做到这一点。如果您认为这是一个巨大的禁忌,请分享您的意见! :P
这是我的 FragmentBase 类,它解决了这个问题:
Update 12/17/11:
I actually found the correct way to save the content of the Fragments. You must use
FragmentStatePagerAdapter
. This adapter properly saves the state of the fragment! :)OLD:
Well I found a way to do this.. Please share your input if you believe this is a huge no no! :P
Here is my FragmentBase class that fixed this issue: