ViewPager与纵向和横向不同的适配器 [英] ViewPager with different adapters for portrait and landscape

查看:484
本文介绍了ViewPager与纵向和横向不同的适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在纵向模式下,我的 ViewPager 有3个片段A,B,C,但在横向模式下,它只有2片段A和C.所以我创建2 FragmentStatePagerAdapter S代表每种模式。问题是,当屏幕方向改变, ViewPager 恢复,并使用旧取向previous片段。例如,当改变方向从纵向到横向, ViewPager 现在显示2片段A的,B不是A和C.我知道为什么发生这种情况,但无法找到一个很好的解决这一点。

In portrait mode, my ViewPager has 3 fragments A, B, C but in landscape mode, it has only 2 fragments A and C. So I create 2 FragmentStatePagerAdapters for each mode. The problem is when screen orientation changed, ViewPager restores and uses previous fragments of old orientation. For example, when change orientation from portrait to landscape, ViewPager now shows 2 fragments A, B instead of A and C. I know why this happen but can't find a good solution for this.

我目前的解决方法是使用不同的ID为 ViewPager (例如:ID / viewpager_portrait的纵向和横向布局ID / viewpager_landscape)重用片段prevent但是这引起了我的内存泄漏,因为旧片段不会被破坏,但仍然可以保存在内存中。

My current workaround is to use different ids for ViewPager (eg: id/viewpager_portrait for portrait and id/viewpager_landscape for landscape layout) to prevent from reusing fragments but this cause me a memory leak because old fragment will not be destroyed and still be kept in memory.

我已经尝试了一些解决方法就像在活动的的onCreate ,或删除的片段 ViewPager 活动中的的onSaveInstanceState ,但他们都让我的应用程序崩溃。

I have tried some workaround like call super.onCreate(null) in activity's onCreate, or remove fragments of ViewPager in activity's onSaveInstanceState but they all makes my app crash.

所以我的问题是,如何避免重复使用的一个或多个片段 FragmentStatePagerAdapter 取向时改变?

So my question is how to avoid reusing one or many fragments in FragmentStatePagerAdapter when orientation changed?

任何帮助将AP preciated。预先感谢。

Any helps will be appreciated. Thank in advance.

推荐答案

这个问题可能是,内置的 PagerAdapter 实现了片段■通过Android的规定假设项目将保持不变,所以保留和再利用基于索引的引用被添加到所有片段 S中的 ViewPager 。这些引用通过 FragmentManager 甚至活动后(和片段 S)被重建,由于配置发生变化或者被杀害的过程。

The issue probably is that the built-in PagerAdapter implementations for Fragments provided by Android assume that the items will remain constant, and so retain and reuse index-based references to all Fragments that are added to the ViewPager. These references are maintained through the FragmentManager even after the Activity (and Fragments) is recreated due to configuration changes or the process being killed.

您需要做的是写自己的实现 PagerAdapter 中的每一个自定义标记关联片段并存储在片段 S IN标签为基础的(而不是基于索引的)格式。你可以添加一个抽象方法提供基于索引旁边的的getItem()方法的标签后,得出从一个现有的通用实现了这一点。当然,你将不得不删除孤立/未使用片段 S IN从previous配置添加了 ViewPager (最好同时持有它的状态)。

What you need to do is to write your own implementation of PagerAdapter that associates a custom tag with each Fragment and stores the Fragments in a tag-based (instead of index-based) format. You could derive a generic implementation of this from one of the existing ones after adding an abstract method for providing a tag based on the index alongside the getItem() method. Of course, you will have to remove orphaned/unused Fragments added in the previous configuration from the ViewPager (while ideally holding on to it's state).

如果你不想自己实现了整体解决方案,那么 ArrayPagerAdapter 中的 CWAC寻呼机库可用于提供合理实施这一举手之劳。在初始化时,你可以分离相关的规定片段基于它的标签,并删除/从适配器添加为好,适当的。

If you don't want to implement the whole solution yourself, then the ArrayPagerAdapter in the CWAC-Pager library can be used to provide a reasonable implementation of this with little effort. Upon initialization, you can detach the relevant Fragment based on it's provided tag, and remove/add it from the adapter as well, as appropriate.

这篇关于ViewPager与纵向和横向不同的适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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