question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: ion-virtual-scroll overlapping items

See original GitHub issue

Bug Report

Ionic version: 4.4

Current behavior: When using a virtual scroll component containing items of varying height with an infinite scroll occasionally the height of some items is approxItemHeight instead of calculating the height which causes overlapping.

Expected behavior: It should be possible to have items of varying heights within a virtual scroll component when using infinite-scroll.

Steps to reproduce: I’ve created a simple test app with which you can reproduce the issue. It’s available here: https://github.com/andsee/Ionic-Virtual-Scroll-Overlap-Bug

Run in chrome while inspecting in a portrait iPhone X and scroll down, keep scrolling until you see some overlapping. Note I’ve set the approxItemHeight to just 10px as this makes it easier to reproduce, it still occurs with larger approximations. Occasionally I’ve had to scroll down to item 200+, other times it happens quickly. It’s probably to do with the speed at which you scroll.

image

Here is a video of it occurring: https://youtu.be/uu_R5L7KIOE Related code: Full project allowing repro: https://github.com/andsee/Ionic-Virtual-Scroll-Overlap-Bug

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    <ion-title>
      Home
      <ion-button (click)="refresh()">Refresh</ion-button>
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
    <ion-virtual-scroll #vScroll [items]=ourItems approxItemHeight="10px">
      <div *virtualItem="let item">
        <app-dynamic-one  [layoutData]="item">
        </app-dynamic-one>
      </div>
    </ion-virtual-scroll>
    <ion-infinite-scroll threshold="100%" (ionInfinite)="infiniteScroll($event)" style="z-index: 99999">
      <ion-infinite-scroll-content style="z-index: 99999;"
              loadingSpinner="bubbles"
              loadingText="Loading more ...">
      </ion-infinite-scroll-content>
    </ion-infinite-scroll>
</ion-content>
import { Component, ViewChild } from '@angular/core';
import { IonVirtualScroll } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

  @ViewChild('vScroll', { read: IonVirtualScroll }) vScroll: IonVirtualScroll;
  ourItems:any[] = []
  constructor()
  {
    this.add(10);
  }

  refresh()
  {
    this.vScroll.checkRange(0);
  }

  add(howMany:number)
  {
    var offset = this.ourItems.length;
    console.log("Add elements "+offset+" to "+(offset+howMany-1));
    for(var i = offset; i < offset+howMany; i++)
    {
      this.ourItems.push({
        index:i
      });
    }
  }

  infiniteScroll(event)
  {
    this.add(10);
    this.vScroll.checkEnd();
    event.target.complete();
  }

}

Other information:

Ionic info:

Ionic:
   ionic (Ionic CLI)             : 4.12.0 (/usr/local/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.4.0
   @angular-devkit/build-angular : 0.13.9
   @angular-devkit/schematics    : 7.3.9
   @angular/cli                  : 7.3.9
   @ionic/angular-toolkit        : 1.5.1

System:
   NodeJS : v10.15.3 (/usr/local/bin/node)
   npm    : 6.9.0
   OS     : macOS High Sierra

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:31 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
zeusstlcommented, Jul 12, 2019

In version 4.6.2, ion-virtual-scroll still has issues. In fact, the update seems to have broken a working ion-virtual-scroll.

I decided to dig in a bit to see if I could isolate the problem since I seem to perpetually have ion-virtual-scroll issues like this:

My code: I removed all of my custom css to make sure it wasn’t causing issues.

<ion-content>
        <ion-virtual-scroll #virtualScroll [items]="sessionsList" [approxItemHeight]="18" [approxHeaderHeight]="1" [approxFooterHeight]="1">
            <ion-card *virtualItem="let session;">
                Hi
            </ion-card>
        </ion-virtual-scroll>
</ion-content>

How it renders: (Note that there are 6 cards instead of 11 showing) Screen Shot 2019-07-11 at 7 06 24 PM

Tried switching to items instead of cards, which helped, but I’m trying to use cards. Not items. Screen Shot 2019-07-11 at 7 05 56 PM

Added a console.log() in: node_modules/@ionic/angular/node_modules/@ionic/core/dist/esm/legacy/ion-virtual-scroll.entry.js

The height should be 18px for each. First render ( in the console log) shows empty. Second render - Note the top field is multiples of 18: Screen Shot 2019-07-11 at 10 13 14 PM Third render - Note the top field is suddenly multiples of 28 for some reason: Screen Shot 2019-07-11 at 10 13 30 PM

The solution for me, for the time being, was to put the ion-card inside of an ion-item.

This is especially ironic since the documentation for ion-virtual-scroll gives the example using a card: https://ionicframework.com/docs/api/virtual-scroll Screen Shot 2019-07-11 at 10 57 31 PM

Hope all of this helps someone find the source of this issue and resolve this once and for all. I feel like I’ve spent more time debugging ion-virtual-scroll since (I think) ionic v1, than debugging anything else in ionic.

4reactions
xXcoronaXxcommented, Jun 5, 2020

Hi everyone this is my fix/workaround for this problem:

.fixOverlapp {
  height: 100% !important;
  overflow: auto;
}
.fixOverlapp >.virtual-item {
  position: initial !important;
  transform: initial !important;
}
<ion-virtual-scroll class="fixOverlapp" [items]="orders">
....
</ion-virtual-scroll>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Ion Virtual Scroll Overlapping Items - Ionic Forum
Hello, I have problems with ion-virtual-scroll, I have seen that an already well known problem and I am trying to fix it.
Read more >
Is Ionic Virtual Scroll very buggy or not? - Stack Overflow
I think I found the solution. The bug was caused by ion tags inside ion-virtual-scroll. So instead of having <ion-virtual-scroll ...
Read more >
Ionic 2: Fix VirtualScroll overlapping items bug
If you're using a VirtualScroll component with variable height items, they can appear overlapped. This is a workaround.
Read more >
Faster List Performance with Ionic 4 Virtual Scroll & Infinite Scroll
In this tutorial we increase our Ionic 4 list performance with the virtual scroll and infinite scroll component! Learn to build mobile apps ......
Read more >
Ionic 2: VirtualScroll items overlapping on first render
Ionic 2 docs specify to wrap custom components in a div in virtual scroll: Custom Components If a custom component is going to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found