Commit 7dcd732
[Android] CollectionView: Defer RemainingItemsThresholdReached to avoid RecyclerView scroll callback warnings (#30907)
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!
### Root Cause
When `RemainingItemsThreshold` is met during a scroll, `OnScrolled`
calls `SendRemainingItemsThresholdReached()` synchronously. This
triggers `notifyItemRangeChanged` on the RecyclerView adapter while
still inside the scroll callback. RecyclerView asserts against adapter
modifications during layout/scroll passes, producing:
```
W/RecyclerView: Cannot call this method in a scroll callback. Scroll callbacks might be run during a measure & layout pass where you cannot change the RecyclerView data.
java.lang.IllegalStateException: ...RecyclerView.assertNotInLayoutOrScroll
```
This affects any scenario where `RemainingItemsThresholdReachedCommand`
(or the `RemainingItemsThresholdReached` event) modifies the bound
collection — including the common pattern of clearing and reloading
items followed by `ScrollTo(0)`.
### Description of Change
Defers `SendRemainingItemsThresholdReached()` until the RecyclerView
transitions to `ScrollStateIdle`, which is a guaranteed safe point
outside any layout/scroll callback.
**Changes in `RecyclerViewScrollListener.cs`:**
- Added `_pendingRemainingItemsThresholdReached` boolean field
- Replaced direct calls to
`_itemsView.SendRemainingItemsThresholdReached()` in `OnScrolled` with
calls to `HandleRemainingItemsThresholdReached()`
- Added private `HandleRemainingItemsThresholdReached()` method that
sets the pending flag
- Added `OnScrollStateChanged` override that fires the deferred event
when `newState == RecyclerView.ScrollStateIdle`, with disposal and null
guards
- Clears the pending flag in `Dispose(bool)` to avoid stale state
**Why `OnScrollStateChanged` / `ScrollStateIdle` instead of `Post()`:**
`recyclerView.Post()` posts to the next Looper tick, which can still be
a layout pass and trigger the same RecyclerView assertion. Using
`ScrollStateIdle` is a stronger guarantee — it signals that RecyclerView
has fully settled and adapter mutations are safe.
### What NOT to Do (for future agents)
- ❌ **Don't call `SendRemainingItemsThresholdReached()` directly in
`OnScrolled`** — RecyclerView
asserts against adapter modifications during scroll/layout passes; this
causes
`IllegalStateException` warnings.
- ❌ **Don't use `recyclerView.Post()`** — `Post()` only defers to the
next Looper tick, which
may still be a layout pass and can trigger the same RecyclerView
assertion. `ScrollStateIdle`
is a stronger, safer guarantee.
### Issues Fixed
Fixes #23030
Fixes #25010
### Platforms Tested
- [x] Android
- [ ] Windows
- [ ] iOS
- [ ] Mac
<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->
### Output
| Before| After|
|--|--|
| <video
src="https://github.com/user-attachments/assets/80c204ed-1657-4303-85de-df3ba998237a">
| <video
src="https://github.com/user-attachments/assets/fc748a87-8419-4fa4-9286-7ba3999b6c97">
|
---------
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>1 parent c4e883a commit 7dcd732
2 files changed
Lines changed: 29 additions & 2 deletions
File tree
- src/Controls/src/Core
- Handlers/Items/Android
- PublicAPI/net-android
Lines changed: 27 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
91 | 92 | | |
92 | 93 | | |
93 | 94 | | |
94 | | - | |
| 95 | + | |
95 | 96 | | |
96 | 97 | | |
97 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
98 | 123 | | |
99 | 124 | | |
100 | 125 | | |
| |||
176 | 201 | | |
177 | 202 | | |
178 | 203 | | |
| 204 | + | |
179 | 205 | | |
180 | 206 | | |
181 | 207 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
0 commit comments