为什么 ViewPager2 延迟加载页面? [英] Why is ViewPager2 delaying to load pages?
问题描述
我决定尝试一下 ViewPager2
新的和现在可用的稳定版本.
I decided to give a try on the new and now available stable release of ViewPager2
.
我的 ViewPager2
有很多页面,我正在使用 TabLayout
为每个页面(片段)提供可滚动的标签标题,但是在加载ViewPager2
在我看来是第一次.
My ViewPager2
have a lot of pages, and I am using TabLayout
to give each page (fragment) scrollable tab titles, but I am facing a delay when loading the ViewPager2
for the first time in my view.
这是正常行为吗?
推荐答案
我找到了导致 ViewPager2
初始延迟加载页面的原因.
I figured it out what was causing the ViewPager2
initial delay to load the pages.
碰巧我使用 com.google.android.material.tabs.TabLayout
中的 TabLayout
和 ViewPager2
来提供可滚动标签每页的标题.但是如果你有很多页面,就像我的情况一样,UI 需要时间来设置每个选项卡并将其放置在 UI 中.
It happens I was using TabLayout
from com.google.android.material.tabs.TabLayout
along with the ViewPager2
to give scrollable tab titles to each page. But if you have a lot of pages which is my case, the UI takes time to setup each tab and place in the UI.
解决办法是从ViewPager2
中去掉TabLayout
,如果需要显示标题,使用ToolBar
作为标题页指示器通过根据您的需要更改和设置 ToolBar
文本来为每个页面.
The solution is to remove the TabLayout
from ViewPager2
, and if you need to display a title, use the ToolBar
as title page indicator for each page by changing and setting ToolBar
text according to your needs.
从好的方面来看,您最终会得到更干净的 UI,因为您的 ViewPager2
在屏幕上有更多空间来向用户显示其内容.
Looking the bright side you end up with more clean UI, by having more space in screen for your ViewPager2
to display its content to the user.
我注意到的另一件事是,如果您想充分利用 ViewPager2
的性能优势,请使用默认的 viewpager 的 offscreenPageLimit,如下所述:
Another thing I noticed is if you want to take full advantage of ViewPager2
performance benefits, then use the default viewpager's offscreenPageLimit, as stated bellow:
public void setOffscreenPageLimit (int limit)
public void setOffscreenPageLimit (int limit)
设置应保留在当前可见页面任一侧的页数.超出此限制的页面将在需要时从适配器重新创建.将此设置为 OFFSCREEN_PAGE_LIMIT_DEFAULT 以使用 RecyclerView 的缓存策略.给定的值必须大于 0 或 #OFFSCREEN_PAGE_LIMIT_DEFAULT.
Set the number of pages that should be retained to either side of the currently visible page(s). Pages beyond this limit will be recreated from the adapter when needed. Set this to OFFSCREEN_PAGE_LIMIT_DEFAULT to use RecyclerView's caching strategy. The given value must either be larger than 0, or #OFFSCREEN_PAGE_LIMIT_DEFAULT.
在远离当前页面的限制页面内的页面被创建并添加到视图层次结构中,即使它们在屏幕上不可见.超出此限制的页面将从视图层次结构中删除,但 RecyclerView 将照常回收 ViewHolders.
Pages within limit pages away from the current page are created and added to the view hierarchy, even though they are not visible on the screen. Pages outside this limit will be removed from the view hierarchy, but the ViewHolders will be recycled as usual by RecyclerView.
这是作为优化提供的.如果您事先知道需要支持的页面数量或在页面上设置延迟加载机制,则调整此设置可以提高分页动画和交互的感知平滑度.如果您有少量页面 (3-4) 可以同时保持活动状态,那么随着用户页面来回切换,新创建的视图子树的布局将花费更少的时间.
This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.
您应该将此限制保持在较低水平,尤其是当您的页面具有复杂的布局时.默认情况下,它设置为 OFFSCREEN_PAGE_LIMIT_DEFAULT.
You should keep this limit low, especially if your pages have complex layouts. By default it is set to OFFSCREEN_PAGE_LIMIT_DEFAULT.
话虽如此,我强烈建议在所有情况下将您的 ViewPager
替换为 ViewPager2
,因为 ViewPager
不再接受 Google 支持,和 ViewPager2
除了我在这里所说的之外,还有很多东西可以提供:
That being said I strongly recommend the replacement of your ViewPager
to ViewPager2
for all cases, because ViewPager
is no longer receiving Google support, and ViewPager2
has a lot to offer other than what I spoke here:
RTL(从右到左)布局支持
RTL (right-to-left) layout support
垂直方向支持
可靠的 Fragment 支持(包括处理对底层 Fragment 集合的更改)
Reliable Fragment support (including handling changes to the underlying Fragment collection)
数据集变化动画(包括 DiffUtil 支持)
Dataset change animations (including DiffUtil support)
https://developer.android.com/jetpack/androidx/releases/viewpager2
这篇关于为什么 ViewPager2 延迟加载页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!