我正在使用包含 3 个片段的 ViewPager,每个片段加载一个列表.问题是当应用程序第一次运行并且我滑动到下一个片段时,这个片段需要一些时间来加载(大约 2 秒),然后它的视图才可见.这是一个非常奇怪的行为.我想要的只是一旦应用程序启动,ViewPager 中的所有片段都应该为用户准备好,所以当他们刷过片段时,没有等待时间.我怎样才能做到这一点?
I'm using ViewPager that holds 3 fragments, each fragment loads a list. The problem is when the application runs for the first time and I swipe to the next fragment, this fragment needs sometime to load (about 2 seconds) before its view is visible. This is a very weird behavior. All I want is once the app has started, all fragments in ViewPager should be ready for user so when they swipe through fragments, there's no wait time. How can I do that?
只要调用setOffscreenPageLimit()
在 onCreate() 中(在初始化 ViewPager 之后).OffscreenPageLimit 设置应保留到当前页面任一侧的页面数(在您的情况下为 2).这样,您的所有片段都将被实例化.
Just call setOffscreenPageLimit()
in onCreate() (after initializing ViewPager). The OffscreenPageLimit sets the number of pages that should be retained to either side of the current page (in your case 2). With this all of your fragments will be instantiate.
(另一种(强烈推荐)可能性是提高列表或列表适配器的性能,因为 2 秒的加载时间听起来不太好)
(An other (highly recommended) possibility is to increase the performance of your lists or listadapters, because a loading time of 2 seconds doesn't sound good)
这篇关于首次运行时在 ViewPager 中预加载片段视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!