@@ -129,7 +129,7 @@ public void ClearRealizedRange(int realizedIndex, int count)
129
129
{
130
130
for ( int i = 0 ; i < count ; i ++ )
131
131
{
132
- // Clear from the edges so that ItemsRepeater can optimize on maintaining
132
+ // Clear from the edges so that ItemsRepeater can optimize on maintaining
133
133
// realized indices without walking through all the children every time.
134
134
int index = realizedIndex == 0 ? realizedIndex + i : ( realizedIndex + count - 1 ) - i ;
135
135
var elementRef = _realizedElements [ index ] ;
@@ -212,7 +212,7 @@ public bool IsDataIndexRealized(int index)
212
212
public ILayoutable GetRealizedElement ( int dataIndex )
213
213
{
214
214
return IsVirtualizingContext ?
215
- GetAt ( GetRealizedRangeIndexFromDataIndex ( dataIndex ) ) :
215
+ GetAt ( GetRealizedRangeIndexFromDataIndex ( dataIndex ) ) :
216
216
_context . GetOrCreateElementAt (
217
217
dataIndex ,
218
218
ElementRealizationOptions . ForceCreate | ElementRealizationOptions . SuppressAutoRecycle ) ;
@@ -252,7 +252,6 @@ public bool IsWindowConnected(in Rect window, ScrollOrientation orientation, boo
252
252
( orientation == ScrollOrientation . Vertical ? ScrollOrientation . Horizontal : ScrollOrientation . Vertical ) :
253
253
orientation ;
254
254
255
-
256
255
var windowStart = effectiveOrientation == ScrollOrientation . Vertical ? window . Y : window . X ;
257
256
var windowEnd = effectiveOrientation == ScrollOrientation . Vertical ? window . Y + window . Height : window . X + window . Width ;
258
257
var firstElementStart = effectiveOrientation == ScrollOrientation . Vertical ? firstElementBounds . Y : firstElementBounds . X ;
@@ -273,53 +272,53 @@ public void DataSourceChanged(object source, NotifyCollectionChangedEventArgs ar
273
272
switch ( args . Action )
274
273
{
275
274
case NotifyCollectionChangedAction . Add :
276
- {
277
- OnItemsAdded ( args . NewStartingIndex , args . NewItems . Count ) ;
278
- }
279
- break ;
275
+ {
276
+ OnItemsAdded ( args . NewStartingIndex , args . NewItems . Count ) ;
277
+ }
278
+ break ;
280
279
281
280
case NotifyCollectionChangedAction . Replace :
282
- {
283
- int oldSize = args . OldItems . Count ;
284
- int newSize = args . NewItems . Count ;
285
- int oldStartIndex = args . OldStartingIndex ;
286
- int newStartIndex = args . NewStartingIndex ;
287
-
288
- if ( oldSize == newSize &&
289
- oldStartIndex == newStartIndex &&
290
- IsDataIndexRealized ( oldStartIndex ) &&
291
- IsDataIndexRealized ( oldStartIndex + oldSize - 1 ) )
292
281
{
293
- // Straight up replace of n items within the realization window.
294
- // Removing and adding might causes us to lose the anchor causing us
295
- // to throw away all containers and start from scratch.
296
- // Instead, we can just clear those items and set the element to
297
- // null (sentinel) and let the next measure get new containers for them.
298
- var startRealizedIndex = GetRealizedRangeIndexFromDataIndex ( oldStartIndex ) ;
299
- for ( int realizedIndex = startRealizedIndex ; realizedIndex < startRealizedIndex + oldSize ; realizedIndex ++ )
282
+ int oldSize = args . OldItems . Count ;
283
+ int newSize = args . NewItems . Count ;
284
+ int oldStartIndex = args . OldStartingIndex ;
285
+ int newStartIndex = args . NewStartingIndex ;
286
+
287
+ if ( oldSize == newSize &&
288
+ oldStartIndex == newStartIndex &&
289
+ IsDataIndexRealized ( oldStartIndex ) &&
290
+ IsDataIndexRealized ( oldStartIndex + oldSize - 1 ) )
300
291
{
301
- var elementRef = _realizedElements [ realizedIndex ] ;
302
-
303
- if ( elementRef != null )
292
+ // Straight up replace of n items within the realization window.
293
+ // Removing and adding might causes us to lose the anchor causing us
294
+ // to throw away all containers and start from scratch.
295
+ // Instead, we can just clear those items and set the element to
296
+ // null (sentinel) and let the next measure get new containers for them.
297
+ var startRealizedIndex = GetRealizedRangeIndexFromDataIndex ( oldStartIndex ) ;
298
+ for ( int realizedIndex = startRealizedIndex ; realizedIndex < startRealizedIndex + oldSize ; realizedIndex ++ )
304
299
{
305
- _context . RecycleElement ( elementRef ) ;
306
- _realizedElements [ realizedIndex ] = null ;
300
+ var elementRef = _realizedElements [ realizedIndex ] ;
301
+
302
+ if ( elementRef != null )
303
+ {
304
+ _context . RecycleElement ( elementRef ) ;
305
+ _realizedElements [ realizedIndex ] = null ;
306
+ }
307
307
}
308
308
}
309
+ else
310
+ {
311
+ OnItemsRemoved ( oldStartIndex , oldSize ) ;
312
+ OnItemsAdded ( newStartIndex , newSize ) ;
313
+ }
309
314
}
310
- else
311
- {
312
- OnItemsRemoved ( oldStartIndex , oldSize ) ;
313
- OnItemsAdded ( newStartIndex , newSize ) ;
314
- }
315
- }
316
- break ;
315
+ break ;
317
316
318
317
case NotifyCollectionChangedAction . Remove :
319
- {
320
- OnItemsRemoved ( args . OldStartingIndex , args . OldItems . Count ) ;
321
- }
322
- break ;
318
+ {
319
+ OnItemsRemoved ( args . OldStartingIndex , args . OldItems . Count ) ;
320
+ }
321
+ break ;
323
322
324
323
case NotifyCollectionChangedAction . Reset :
325
324
ClearRealizedRange ( ) ;
@@ -376,7 +375,7 @@ private void DiscardElementsOutsideWindow(in Rect window, ScrollOrientation orie
376
375
int backCutoffIndex = realizedRangeSize ;
377
376
378
377
for ( int i = 0 ;
379
- i < realizedRangeSize &&
378
+ i < realizedRangeSize &&
380
379
! Intersects ( window , _realizedElementLayoutBounds [ i ] , orientation ) ;
381
380
++ i )
382
381
{
@@ -391,7 +390,7 @@ private void DiscardElementsOutsideWindow(in Rect window, ScrollOrientation orie
391
390
-- backCutoffIndex ;
392
391
}
393
392
394
- if ( backCutoffIndex < realizedRangeSize - 1 )
393
+ if ( backCutoffIndex < realizedRangeSize - 1 )
395
394
{
396
395
ClearRealizedRange ( backCutoffIndex + 1 , realizedRangeSize - backCutoffIndex - 1 ) ;
397
396
}
@@ -419,14 +418,14 @@ private void OnItemsAdded(int index, int count)
419
418
// to insert items.
420
419
int lastRealizedDataIndex = _firstRealizedDataIndex + GetRealizedElementCount ( ) - 1 ;
421
420
int newStartingIndex = index ;
422
- if ( newStartingIndex > _firstRealizedDataIndex &&
421
+ if ( newStartingIndex >= _firstRealizedDataIndex &&
423
422
newStartingIndex <= lastRealizedDataIndex )
424
423
{
425
424
// Inserted within the realized range
426
425
int insertRangeStartIndex = newStartingIndex - _firstRealizedDataIndex ;
427
426
for ( int i = 0 ; i < count ; i ++ )
428
427
{
429
- // Insert null (sentinel) here instead of an element, that way we dont
428
+ // Insert null (sentinel) here instead of an element, that way we dont
430
429
// end up creating a lot of elements only to be thrown out in the next layout.
431
430
int insertRangeIndex = insertRangeStartIndex + i ;
432
431
int dataIndex = newStartingIndex + i ;
0 commit comments