prevent ViewPager毁坏离屏幕视图视图、屏幕、prevent、ViewPager

2023-09-12 08:28:39 作者:素顏

我有一个ViewPager迷上了一个FragmentPagerAdapter多数民众赞成显示三个片段。所述ViewPager似乎破坏托管片段的观点时,它是一个以上的滑动距离的当前位置。

I have a ViewPager hooked up to a FragmentPagerAdapter that's displaying three fragments. The ViewPager appears to destroy a hosted fragment's view when it is more than one swipe away from the current position.

这些观点都是简单的列表,这个优化是完全没有必要的,所以我想将其禁用。这是造成一些视觉上的问题,因为列表应用了布局动画,这些动画被重播,他们已经被摧毁,重建后。它也显示了滚动条介绍动画每次(其中滚动条是简要地可见,表示滚动是可能的),可以分散注意力,和用户的当前滚动位置是失去了在该过程

These views are all simple lists and this optimization is completely unnecessary, so I'd like to disable it. It's causing some visual problems because the lists have layout animations applied to them and those animations are being replayed after they've been destroyed and recreated. It also shows the scrollbar intro animation each time (where the scrollbar is briefly visible to indicate that scrolling is possible) which can be distracting, and the user's current scroll position is lost in the process.

它也不会加载第三个片段,直到第一个刷卡情况,这是有问题的,因为每一个片段处理自己的服务电话和我最好preFER活动时有三个关火的同时负载。具有第三服务呼叫延迟不太理想。

It also doesn't load the third fragment until the first swipe happens, which is problematic because each fragment handles its own service calls and I'd prefer to have all three fire off at the same time when the activity loads. Having the third service call delayed is less than ideal.

有什么办法说服ViewPager制止这种行为,只是让我所有的片段记忆?

Is there any way to convince ViewPager to stop this behavior and just keep all my fragments in memory?

推荐答案

在修订4 的支持包,一个方法被添加到ViewPager它允许您指定屏幕外页的数量来使用,而不是它是默认的1。

In revision 4 of the Support Package, a method was added to ViewPager which allows you to specify the number of offscreen pages to use, rather than the default which is 1.

在你的情况,你要指定2,这样当你在第三页上,第一个不被破坏,反之亦然。

In your case, you want to specify 2, so that when you are on the third page, the first one is not destroyed, and vice-versa.

mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);