嵌套viewpager的setCurrentItem [英] setCurrentItem of nested viewpager

查看:209
本文介绍了嵌套viewpager的setCurrentItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewPager 嵌套在一个片段,当设备改变方向,我想保存和恢复的页面的 ViewPager 开始了。我目前在做这样的:

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    mViewPager =(ViewPager)inflater.inflate(R.layout.activity_albumpicker,集装箱,FALSE);    //创建将返回一个片段为三个的适配器
    //活性的主要部分。
    mSectionsPagerAdapter =新SectionsPagerAdapter(getChildFragmentManager());    setRetainInstance(真);    //设置的ViewPager与部分适配器。
    mViewPager.setAdapter(mSectionsPagerAdapter);    如果(savedInstanceState!= NULL){
        mViewPager.setCurrentItem(savedInstanceState.getInt(pagerState),FALSE);
    }
    返回mViewPager;
}@覆盖
公共无效的onSaveInstanceState(捆绑outState){
    super.onSaveInstanceState(outState);
    outState.putInt(pagerState,mViewPager.getCurrentItem());
}

的onSaveInstanceState 的值被保存是正确的,并在 onCreateView 旋转<$ C $的值之后C> savedInstanceState.getInt(pagerState)也是正确的。但没有任何反应,在 ViewPager 保持其默认页面上,并没有什么在logcat中。我难倒。


解决方案

几个小时的审查一定的东西后,我想出了下述溶液中(这是做的,我想一般的方式)。我有两个步骤,您已经做了和其他必要的步骤。



    1. 呼叫 setRetainInstance(真); (你这样做)



  • 设置片段的节能状态。 (你已经这样做了)



  • 在主机活动,请拨打以下,以确保您的片段已经保存在活动的状态:


这里是一个片段:

  ...(其他东西)片段mainFragment; //定义本地成员 @覆盖
 保护无效的onCreate(捆绑savedInstanceState){
   super.onCreate(savedInstanceState);   //当你旋转屏幕,默认情况下你的活动将重新创建
   //在这一点备用状态,即一捆,savedInstanceState是
   //一般不为null。如果你调试它,你可以看到它是如何保存
   //最新FragmentTransactionState,占据着你的碎片的实例   //为安全起见,我们在这里检查,并检索片段实例。
   mainFragment = getSupportFragmentManager()findFragmentById(android.R.id.content)。
   如果(mainFragment == NULL){     //非常首创,您的片段是空明​​显,所以我们需要
     //创建它,并将它添加到该事务。但在旋转后,它的
     //实例很好地保存由主机活动,这样你就不需要做
     //什么。     mainFragment =新MainFragment();
     getSupportFragmentManager()调用BeginTransaction()
         .replace(android.R.id.content,mainFragment)
         。承诺();
   }
 }

这就是我做的,使它的工作原理。希望这有助于。

I have a ViewPager nested in a fragment, and when the device changes orientation I'd like to save and restore which page the ViewPager was on. I'm currently doing this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mViewPager = (ViewPager) inflater.inflate(R.layout.activity_albumpicker, container, false);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());

    setRetainInstance(true);

    // Set up the ViewPager with the sections adapter.
    mViewPager.setAdapter(mSectionsPagerAdapter);

    if(savedInstanceState != null) {
        mViewPager.setCurrentItem(savedInstanceState.getInt("pagerState"), false);
    }
    return mViewPager;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("pagerState", mViewPager.getCurrentItem());
}

during onSaveInstanceState the value being saved is correct, and in onCreateView after rotating the value of savedInstanceState.getInt("pagerState") is also correct. But nothing happens, the ViewPager stays on its default page, and there's nothing in the logcat. I'm stumped.

解决方案

After couple hours of reviewing some certain stuffs, I come up with the following solution (which is a general way to do I suppose). I include both steps You've done and other necessary steps.

    1. Call setRetainInstance(true); (You've done this)

    1. Setup Fragment's saving states. (You've done this)

    1. On Host Activity, call the following to make sure your Fragment has been saved in Activity's state:

Here is a snippet:

... (other stuffs)

Fragment mainFragment; // define a local member

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   // After you rotate your screen, by default your Activity will be recreated     
   // with a bundle of saved states, i.e at this point, savedInstanceState is    
   // not null in general. If you debug it, you could see how it saved your 
   // latest FragmentTransactionState, which hold your Fragment's instance

   // For safety, we check it here, and retrieve the fragment instance.
   mainFragment =    getSupportFragmentManager().findFragmentById(android.R.id.content);
   if (mainFragment == null) { 

     // in very first creation, your fragment is null obviously, so we need to  
     // create it and add it to the transaction. But after the rotation, its 
     // instance is nicely saved by host Activity, so you don't need to do 
     // anything.

     mainFragment = new MainFragment();
     getSupportFragmentManager().beginTransaction()
         .replace(android.R.id.content, mainFragment)
         .commit();
   }
 }

That is all I do to make it works. Hope this helps.

这篇关于嵌套viewpager的setCurrentItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆