ViewPager + FragmentStatePagerAdapter +方向变化 [英] ViewPager + FragmentStatePagerAdapter + orientation change

查看:125
本文介绍了ViewPager + FragmentStatePagerAdapter +方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题:我有一个ViewPager一些网页我用的是FragmentStatePagerAdapter来处理数据。在人像我有例如20页的ViewPager和景观,我只10页的ViewPager。因此,在一个方向变化我创建一个新的适配器与不同的数据。

I have a little Problem: i have a ViewPager with some Pages and i use the FragmentStatePagerAdapter to handle the data. In portrait i have for example 20 pages for the ViewPager and in landscape i have just 10 pages for the ViewPager. So on a orientation change i create a new Adapter with different Data.

下面解释一下原因:我告诉人像一个图像和两个景观,但始终都在计数。如果我有10张图片显示,我有景观(总二)10在纵向和5。

Here a little explanation why: I show one image in portrait and two in landscape but always all in count. If i have 10 pictures to show, i have 10 in portrait and 5 in landscape (always two).

但现在我有一个奇怪的错误: 当我在景观上指数5,然后打开设备时,ViewPager的当前页面设置为10。如果我把它再回来我是第5页。如果我刷卡的ViewPage现在至10页适配器的梅索德的getItem是从来没有所谓的的ViewPage显示我一个人像图片,而不是两个景观。 怎么能这样呢?有没有在适配器或ViewPager缓存?在为新创建的活动一切的OnCreate,适配器也是数据(只是字符串与路径)ViewPager。 因此,任何想法如何解决这一问题真正可怕的功能?

But now i have a strange bug: When i am in Landscape on index 5 and turn the device, the ViewPager's current page is set to 10. If i turn it back again i am on page 5. If i swipe the ViewPage now to page 10 the methode getItem of the adapter is never called and the ViewPage shows me the one portrait picture and not the two for landscape. How can this happen? Is there a cache in the adapter or ViewPager? In the onCreate of the Activity everything is new created, the adapter and also the data (just strings with path) for the ViewPager. So any idea how to fix this really scary "feature"?

下面是一些code:

的onCreate:

    mViewPagerAdapter = new ReaderPageViewAdapter(getSupportFragmentManager(), getBaseContext(), mCurrentDocument.mPages, getResources()
                .getConfiguration().orientation);
    mPageReader = (ReaderViewPager) findViewById(R.id.pager);
    mPageReader.setAdapter(mViewPagerAdapter);

适配器的getItem:

adapter getItem:

    public Fragment getItem(final int index) {
        final PageInfo pageInfo = mPages.get(index);
        final PageFragment pageFragment = (PageFragment) PageFragment
                .instantiate(mContext, pageInfo.mClss.getName(), pageInfo.mArgs);
        return pageFragment;
    }

如果你需要更多的东西告诉我。谢谢

If you need something more just tell me. Thanks

推荐答案

您可以覆盖FragmentActivity的onSaveInstanceState(),而不是调用super.onSaveInstanceState()的方法。

You can override the FragmentActivity onSaveInstanceState() and not call the super.onSaveInstanceState() in the method.

@Override
protected void onSaveInstanceState(final Bundle outState) {
    // super.onSaveInstanceState(outState);
}

不要使用 mViewPager.setSaveEnabled(假);

它结束了一个大memoryLeak。取向的每一个变化之后,它把碎片进入FragmentManager的数组,从来没有清理它。因此,它上升到100MB内存的使用情况后改变方向多次。使用的onSaveInstanceState方法是一种更好的方式,我认为。

it ended up with a big memoryLeak. After every change of the orientation it puts the Fragments into an array of FragmentManager and never cleaned it. So it goes up to over 100mb memory usage after change the orientation many times. Using the onSaveInstanceState method is a better way I think.

这篇关于ViewPager + FragmentStatePagerAdapter +方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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