title: Maintain scroll position with CDK autosize virtual scroll strategy
body: <p>Maintain scroll position with CDK autosize virtual scroll strategy</p>
  <p>I have a large list of items that can be scrolled with <code>&lt;cdk-virtual-scroll-viewport autosize&gt;</code> provided by <code>@angular/cdk-experimental</code> (the items have dynamic heights, thus I'm using this scroll strategy instead of the <code>FixedSizeVirtualScrollStrategy</code>).</p>
  <p>New items are inserted in the list over time, i.e. they are appended on top. When the user scrolls down I want to avoid that new items will push the items in the viewport away. Therefore I need a mechanism to maintain / restore the scroll position after the items have been added.</p>
  <p>I have a semi-working solution (and hacky because it reads private fields), but the viewport shifts a few pixels randomly after items have been added.</p>
  <p>Here are the relevant code parts:</p>
  <pre><code>@ViewChild(CdkVirtualScrollViewport) viewport: CdkVirtualScrollViewport;
  
  ...
  
  // After new items have been added:
  
  const offset = this.viewport.measureScrollOffset('top');
  const topPosition = offset + newItems.length * this.viewport['_scrollStrategy']._averager._averageItemSize; // the autosize strategy determines an average item size I'm trying to use to determine how the viewport should be shifted
  
  this.viewport.scrollTo({
    top: topPosition
  });
  </code></pre>
  <p>I created a demo of this approach here: <a href="https://stackblitz.com/edit/angular-be926g?file=src/app/cdk-virtual-scroll-overview-example.ts" rel="nofollow noreferrer">https://stackblitz.com/edit/angular-be926g?file=src/app/cdk-virtual-scroll-overview-example.ts</a></p>
  <p>Any ideas how to achieve this seamlessly with a constant viewport?</p>