Preload fragment#39;s view in ViewPager on first run(首次运行时在 ViewPager 中预加载片段视图)
问题描述
我正在使用包含 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 中预加载片段视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:首次运行时在 ViewPager 中预加载片段视图
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
