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

查看:25
本文介绍了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 中是否有缓存?在 Activity 的 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"?

这里有一些代码:

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(false);

它最终导致了一个很大的 memoryLeak.每次更改方向后,它都会将 Fragment 放入 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天全站免登陆