Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
20b30b5
Prepare normalized integration ancestry for 11.0.1xx SR2
github-actions[bot] Nov 10, 2026
b24f053
[Android]Fix keyboard-type flicker when replacing focused ContentView…
SuthiYuvaraj Nov 13, 2026
206972c
[Windows] Fix Boxview shadow is not aligned with box view shape (#40175)
SubhikshaSf4851 Nov 13, 2026
b88d3ef
[net11] Recover lost Android emulator before UI test retry (#40273)
kubaflo Nov 16, 2026
1624c52
[net11] Fail fast on Appium session creation after the device is lost…
kubaflo Nov 16, 2026
66fce23
[Android] Fix CollectionView ItemSpacing when items are added or remo…
marcelomoresco Nov 16, 2026
ccf4b52
[Android] Invalidate ItemSpacing only on structural adapter changes
marcelomoresco Nov 17, 2026
1843dbb
Prepare normalized integration ancestry for 11.0.1xx SR2
github-actions[bot] Nov 10, 2026
3e0f22a
[Android]Fix keyboard-type flicker when replacing focused ContentView…
SuthiYuvaraj Nov 13, 2026
219a32f
[Windows] Fix Boxview shadow is not aligned with box view shape (#40175)
SubhikshaSf4851 Nov 13, 2026
8fd2bac
[net11] Recover lost Android emulator before UI test retry (#40273)
kubaflo Nov 16, 2026
2620692
[net11] Fail fast on Appium session creation after the device is lost…
kubaflo Nov 16, 2026
e4378ff
Add Android benchmarks for CollectionView ItemSpacing
marcelomoresco Nov 17, 2026
75901d3
[Android] Invalidate ItemSpacing only for changes in the last row
marcelomoresco Nov 17, 2026
605cb84
Don't keep a reference to the observed adapter in DataChangeObserver
marcelomoresco Nov 17, 2026
e318f6e
Merge net11 into fix/android-collectionview-itemspacing-runtime-items
marcelomoresco Nov 17, 2026
04f1de1
Skip the spacing refresh when only the cross-axis spacing is set and …
marcelomoresco Nov 17, 2026
feb9e59
Check the spacing before counting items for the last-row filter
marcelomoresco Nov 17, 2026
7c3c1f7
Test that a grouped insert without scrolling-axis spacing doesn't cou…
marcelomoresco Nov 18, 2026
680bd72
Track the item count in the spacing observer instead of reading it pe…
marcelomoresco Nov 18, 2026
f5bbe7e
Read the item count for each last-row check instead of caching it
marcelomoresco Nov 18, 2026
1129a3b
Take the spacing observer's item count from each layout pass
marcelomoresco Nov 18, 2026
0733466
Test that an off-screen middle change doesn't recompute the visible s…
marcelomoresco Nov 18, 2026
36d0085
Merge net11 into fix/android-collectionview-itemspacing-runtime-items
marcelomoresco Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions src/Controls/src/Core/Handlers/Items/Android/DataChangeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ internal class DataChangeObserver : RecyclerView.AdapterDataObserver
{
IntPtr _adapter;
readonly Action _onDataChange;
readonly Func<int> _lastRowSize;

public bool Observing { get; private set; }

public DataChangeObserver(Action onDataChange) : base()
// Only used with lastRowSize: the adapter's item count as of the last layout pass, kept up to date with the insertions
// and removals since then, or -1 when unknown
public int ItemCount { get; set; } = -1;

// lastRowSize: when set, only report items inserted into or removed from the last row (that many positions, 0 for none)
public DataChangeObserver(Action onDataChange, Func<int> lastRowSize = null) : base()
{
_onDataChange = onDataChange;
_lastRowSize = lastRowSize;
}

public void Start(Adapter adapter)
Expand All @@ -28,6 +35,7 @@ public void Start(Adapter adapter)
adapter.RegisterAdapterDataObserver(this);

_adapter = adapter.Handle;
ItemCount = -1;
Observing = true;
}

Expand All @@ -45,37 +53,80 @@ public void Stop(Adapter adapter)
public override void OnChanged()
{
base.OnChanged();
_onDataChange?.Invoke();
ItemCount = -1;

if (_lastRowSize is null)
{
_onDataChange?.Invoke();
}
}

public override void OnItemRangeInserted(int positionStart, int itemCount)
{
base.OnItemRangeInserted(positionStart, itemCount);
_onDataChange?.Invoke();

if (ItemCount >= 0)
{
ItemCount += itemCount;
}

if (_lastRowSize is null || ReachesLastRow(positionStart + itemCount))
{
_onDataChange?.Invoke();
}
}

public override void OnItemRangeChanged(int positionStart, int itemCount)
{
base.OnItemRangeChanged(positionStart, itemCount);
_onDataChange?.Invoke();

if (_lastRowSize is null)
{
_onDataChange?.Invoke();
}
}

public override void OnItemRangeChanged(int positionStart, int itemCount, Object payload)
{
base.OnItemRangeChanged(positionStart, itemCount, payload);
_onDataChange?.Invoke();

if (_lastRowSize is null)
{
_onDataChange?.Invoke();
}
}

public override void OnItemRangeRemoved(int positionStart, int itemCount)
{
base.OnItemRangeRemoved(positionStart, itemCount);
_onDataChange?.Invoke();

if (ItemCount >= 0)
{
ItemCount -= itemCount;
}

if (_lastRowSize is null || ReachesLastRow(positionStart))
{
_onDataChange?.Invoke();
}
}

public override void OnItemRangeMoved(int fromPosition, int toPosition, int itemCount)
{
base.OnItemRangeMoved(fromPosition, toPosition, itemCount);
_onDataChange?.Invoke();

if (_lastRowSize is null)
{
_onDataChange?.Invoke();
}
}

// True when a range ending at `position` (exclusive) reaches the last row. Without a count from a layout pass yet,
// report the change rather than risk stale spacing.
bool ReachesLastRow(int position)
{
int lastRowSize = _lastRowSize();
return lastRowSize > 0 && (ItemCount < 0 || position > ItemCount - lastRowSize);
}

bool IsValidAdapter(Adapter adapter)
Expand Down
29 changes: 29 additions & 0 deletions src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class MauiRecyclerView<TItemsView, TAdapter, TItemsViewSource> : Recycler
EmptyViewAdapter _emptyViewAdapter;
readonly DataChangeObserver _emptyCollectionObserver;
readonly DataChangeObserver _itemsUpdateScrollObserver;
readonly DataChangeObserver _itemSpacingObserver;
readonly Func<MotionEvent, bool> _dispatchTouchEventToRecyclerView;
ParentScrollGestureDispatcher _parentScrollGestureDispatcher;

Expand All @@ -62,6 +63,9 @@ public MauiRecyclerView(Context context, Func<IItemsLayout> getItemsLayout, Func

_emptyCollectionObserver = new DataChangeObserver(UpdateEmptyViewVisibility);
_itemsUpdateScrollObserver = new DataChangeObserver(AdjustScrollForItemUpdate);
// RecyclerView recomputes the offsets of items it rebinds or shifts (and of every item on a full reset), so only
// insertions and removals in the last row can leave items that kept their position with stale edge spacing
_itemSpacingObserver = new DataChangeObserver(InvalidateItemSpacing, GetSpacingLastRowSize);
_dispatchTouchEventToRecyclerView = DispatchTouchEventToRecyclerView;
_parentScrollGestureDispatcher = new ParentScrollGestureDispatcher(this);
}
Expand Down Expand Up @@ -152,6 +156,7 @@ public virtual void TearDownOldElement(TItemsView oldElement)
// Stop watching for empty items or scroll adjustments
_emptyCollectionObserver.Stop(ItemsViewAdapter);
_itemsUpdateScrollObserver.Stop(ItemsViewAdapter);
_itemSpacingObserver.Stop(ItemsViewAdapter);

// Unhook whichever adapter is active
SetAdapter(null);
Expand Down Expand Up @@ -308,8 +313,10 @@ public virtual void UpdateAdapter()
var oldItemViewAdapter = ItemsViewAdapter;

_emptyCollectionObserver.Stop(oldItemViewAdapter);
_itemSpacingObserver.Stop(oldItemViewAdapter);

ItemsViewAdapter = CreateAdapter();
_itemSpacingObserver.Start(ItemsViewAdapter);

(RecyclerViewScrollListener as RecyclerViewScrollListener<TItemsView, TItemsViewSource>)?.UpdateAdapter(ItemsViewAdapter);

Expand Down Expand Up @@ -628,6 +635,24 @@ protected virtual ItemDecoration CreateSpacingDecoration(IItemsLayout itemsLayou
return new SpacingItemDecoration(Context, itemsLayout);
}

// Only the spacing along the scrolling direction depends on an item's position; the cross-axis spacing is the same for every item
bool HasPositionDependentSpacing() =>
_itemDecoration is SpacingItemDecoration spacing
&& ((ItemsLayout as ItemsLayout)?.Orientation == ItemsLayoutOrientation.Horizontal ? spacing.HorizontalOffset : spacing.VerticalOffset) != 0;

// The number of positions in the last row whose edge spacing can change, or 0 when no spacing depends on the position
int GetSpacingLastRowSize() => HasPositionDependentSpacing() ? (ItemsLayout is GridItemsLayout grid ? grid.Span : 1) : 0;

void InvalidateItemSpacing()
{
// SpacingItemDecoration drops the spacing on the outer edges based on each item's position, but RecyclerView only
// recomputes the offsets of items it rebinds or shifts (not the previous last item on an append, for example)
if (!IsComputingLayout)
{
InvalidateItemDecorations();
}
}

protected virtual void ReconcileFlowDirectionAndLayout()
{
if (!(GetLayoutManager() is LinearLayoutManager linearLayoutManager))
Expand Down Expand Up @@ -711,6 +736,10 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
ClipBounds = new ARect(0, 0, Width, Height);

// The count this layout pass used (ItemCount walks every group of a grouped source, so read it once here rather than
// for every notification); the spacing observer applies the insertions and removals until the next layout pass
_itemSpacingObserver.ItemCount = HasPositionDependentSpacing() ? ItemsViewAdapter?.ItemCount ?? 0 : -1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[moderate] CollectionView - Android - This reads the live adapter count, not a count synchronized with consumed notifications. ItemsSourceFactory leaves observable IEnumerable sources without IList unwrapped, and ObservableItemsSource dispatches their notifications while counting the live source. With one rendered item and ItemSpacing = 8, a worker can append twice after base.OnLayout computes the original item's zero bottom offset but before this read. ItemCount is then seeded with 3 while both insertions are queued. Their callbacks increment it to 4 and 5, so their exclusive ends 2 and 3 both fail ReachesLastRow and neither invalidates the original item's cached offsets. The subsequent layout resets the count without dirtying those offsets, leaving the half-spacing defect. Seed from notification-synchronized state, or preserve the unknown-count fallback when pending notifications cannot be excluded, and cover this layout/source interleaving.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same point as #40318 (comment): base.OnLayout reads the same live count for RecyclerView's own layout, so a source changed off the UI thread in that window is already out of sync with RecyclerView. Keeping it as is.

// After a direct (non-animated) scroll operation, we may need to make adjustments
// to align the target item; if an adjustment is pending, execute it here.
// (Deliberately checking the private member here rather than the property accessor; the accessor will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override void GetItemOffsets(ARect outRect, AView view, RecyclerView pare
var spanSizeLookup = gridLayoutManager.GetSpanSizeLookup();
int spanCount = gridLayoutManager.SpanCount;
isInFirstRowCol = IsInFirstSpanGroup(spanSizeLookup, position, spanCount);
isInLastRowCol = IsInLastSpanGroup(spanSizeLookup, view, position, itemCount, spanCount);
isInLastRowCol = IsInLastSpanGroup(spanSizeLookup, view, state, position, itemCount, spanCount);
}
else
{
Expand Down Expand Up @@ -156,15 +156,15 @@ static bool IsInFirstSpanGroup(GridLayoutManager.SpanSizeLookup spanSizeLookup,
return true;
}

static bool IsInLastSpanGroup(GridLayoutManager.SpanSizeLookup spanSizeLookup, AView view, int position, int itemCount, int spanCount)
static bool IsInLastSpanGroup(GridLayoutManager.SpanSizeLookup spanSizeLookup, AView view, RecyclerView.State state, int position, int itemCount, int spanCount)
{
// Every item occupies at least one span, so the last row holds at most spanCount items.
if (itemCount - 1 - position >= spanCount)
{
return false;
}

int span = GetSpanIndex(spanSizeLookup, view, position, spanCount) + spanSizeLookup.GetSpanSize(position);
int span = GetSpanIndex(spanSizeLookup, view, state, position, spanCount) + spanSizeLookup.GetSpanSize(position);

for (int i = position + 1; i < itemCount; i++)
{
Expand All @@ -182,11 +182,13 @@ static bool IsInLastSpanGroup(GridLayoutManager.SpanSizeLookup spanSizeLookup, A
return true;
}

static int GetSpanIndex(GridLayoutManager.SpanSizeLookup spanSizeLookup, AView view, int position, int spanCount)
static int GetSpanIndex(GridLayoutManager.SpanSizeLookup spanSizeLookup, AView view, RecyclerView.State state, int position, int spanCount)
{
// GridLayoutManager assigns spans to the whole row before measuring its children, so the
// LayoutParams already hold the answer; fall back to the lookup for anything not yet assigned.
if (view.LayoutParameters is GridLayoutManager.LayoutParams layoutParams
// In pre-layout they hold the span of the item's old position, which no longer matches `position`.
if (!state.IsPreLayout
&& view.LayoutParameters is GridLayoutManager.LayoutParams layoutParams
&& layoutParams.SpanIndex != GridLayoutManager.LayoutParams.InvalidSpanId)
{
return layoutParams.SpanIndex;
Expand Down
Loading
Loading