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

Preload fragment#39;s view in ViewPager on first run(首次运行时在 ViewPager 中预加载片段视图)
本文介绍了首次运行时在 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 中预加载片段视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)