简体   繁体   English

首次运行时在ViewPager中预加载片段的视图

[英]Preload fragment's view in ViewPager on first run

I'm using ViewPager that holds 3 fragments, each fragment loads a list. 我使用的ViewPager包含3个片段,每个片段加载一个列表。 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. 问题是,当应用程序首次运行并且我滑动到下一个片段时,此片段需要一段时间(大约2秒)才能加载,其视图才可见。 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. 我想要的就是一旦应用程序启动,ViewPager中的所有片段都应为用户准备就绪,因此当他们在片段中滑动时,没有等待时间。 How can I do that? 我怎样才能做到这一点?

Just call setOffscreenPageLimit() in onCreate() (after initializing ViewPager). 只需在onCreate()中调用setOffscreenPageLimit() (在初始化ViewPager之后)。 The OffscreenPageLimit sets the number of pages that should be retained to either side of the current page (in your case 2). OffscreenPageLimit设置应保留到当前页面任一侧的页面数(在您的情况下为2)。 With this all of your fragments will be instantiate. 这样,您所有的片段都将被实例化。

(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) (另一种( 强烈建议 )的可能性是提高列表或列表适配器的性能,因为2秒的加载时间听起来不太好)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM