android – 片段在刷ViewPager时不调用onSaveInstanceState?

前端之家收集整理的这篇文章主要介绍了android – 片段在刷ViewPager时不调用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状态就好了,但刷卡我无法弄清楚如何正确恢复列表.

解决方法

更新12/17/11:
我实际上找到了保存片段内容的正确方法.您必须使用FragmentStatePagerAdapter.此适配器正确保存片段的状态! 原文链接:https://www.f2er.com/android/315969.html

猜你在找的Android相关文章