Search code examples
androidperformanceandroid-viewpagerandroid-viewpager2

Why is ViewPager2 delaying to load pages?


I decided to give a try on the new and now available stable release of 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.

Is this the normal behavior?


Solution

  • I figured it out what was causing the ViewPager2 initial delay to load the pages.

    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.

    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.

    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.

    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)

    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.

    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.

    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.

    You should keep this limit low, especially if your pages have complex layouts. By default it is set to OFFSCREEN_PAGE_LIMIT_DEFAULT.

    https://developer.android.com/reference/androidx/viewpager2/widget/ViewPager2.html#setOffscreenPageLimit(int)

    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 (right-to-left) layout support

    Vertical orientation support

    Reliable Fragment support (including handling changes to the underlying Fragment collection)

    Dataset change animations (including DiffUtil support)

    https://developer.android.com/jetpack/androidx/releases/viewpager2