dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

Android: CollectionView's ScrollTo() triggers Android warnings #23030

Open softlion opened 3 months ago

softlion commented 3 months ago

Description

Create a CollectionView and bind it to an ObservableCollection filled with enough items so that the CollectionView can be scrolled. Use the UI to scroll down. Then, tap on a button that triggers the deletion of the content of the ObservableCollection and fills it with new items. Finally, programmatically scroll to the top of the CollectionView.

This is working, but triggers scary warnings.

Steps to Reproduce

            await MainThread.InvokeOnMainThreadAsync(async () =>
            {
                //Clear and reload the content of the CollectionView by changing the content of the ObservableCollection bound to the items.
                ...

                //Reset scroll position when filter is changed
                MyCollectionView?.ScrollTo(0, position: ScrollToPosition.Start, animate: false);
            });

Link to public reproduction project repository

No response

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android API 34

Did you find any workaround?

no

Relevant log output

6-13 08:27:10.872 W/RecyclerView( 8912): Cannot call this method in a scroll callback. Scroll callbacks mightbe run during a measure & layout pass where you cannot change theRecyclerView data. Any method call that might change the structureof the RecyclerView or the adapter contents should be postponed tothe next frame.
06-13 08:27:10.872 W/RecyclerView( 8912): java.lang.IllegalStateException:  crc645d80431ce5f73f11.MauiRecyclerView_3{93ab91a VFED..... ......ID 0,0-1080,1679}, adapter:crc645d80431ce5f73f11.ReorderableItemsViewAdapter_2@4afb22, layout:androidx.recyclerview.widget.LinearLayoutManager@3ce958, context:crc640318137a80d29de8.MainActivity@87f00a5
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:3451)
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView$RecyclerViewDataObserver.onItemRangeChanged(RecyclerView.java:6025)
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView$AdapterDataObservable.notifyItemRangeChanged(RecyclerView.java:13128)
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView$AdapterDataObservable.notifyItemRangeChanged(RecyclerView.java:13118)
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView$Adapter.notifyItemRangeChanged(RecyclerView.java:8067)
06-13 08:27:10.872 W/RecyclerView( 8912):   at crc645d80431ce5f73f11.RecyclerViewScrollListener_2.n_onScrolled(Native Method)
06-13 08:27:10.872 W/RecyclerView( 8912):   at crc645d80431ce5f73f11.RecyclerViewScrollListener_2.onScrolled(RecyclerViewScrollListener_2.java:30)
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:5642)
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:4700)
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4326)
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:2065)
06-13 08:27:10.872 W/RecyclerView( 8912):   at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5743)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1339)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1348)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.view.Choreographer.doCallbacks(Choreographer.java:952)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.view.Choreographer.doFrame(Choreographer.java:878)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1322)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.os.Handler.handleCallback(Handler.java:958)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.os.Looper.loopOnce(Looper.java:205)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.os.Looper.loop(Looper.java:294)
06-13 08:27:10.872 W/RecyclerView( 8912):   at android.app.ActivityThread.main(ActivityThread.java:8177)
06-13 08:27:10.872 W/RecyclerView( 8912):   at java.lang.reflect.Method.invoke(Native Method)
06-13 08:27:10.872 W/RecyclerView( 8912):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
06-13 08:27:10.872 W/RecyclerView( 8912):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
github-actions[bot] commented 3 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

QianaJiao commented 2 months ago

I can also repro this error message on the ManualMauiTests demo app (MAUI Version 8.0.70): Lanuch App -> Open I CollectionView -> I9Scrolling -> Incremental loading on scroll -> Check the debug output: [RecyclerView] Cannot call this method in a scroll callback. Scroll callbacks mightbe run during a measure & layout pass where you cannot change theRecyclerView data. Any method call that might change the structureof the RecyclerView or the adapter contents should be postponed tothe next frame. [RecyclerView] java.lang.IllegalStateException: crc645d80431ce5f73f11.MauiRecyclerView_3{e4f97e VFED..... ......ID 0,156-970,1874}, adapter:crc645d80431ce5f73f11.ReorderableItemsViewAdapter2@942d7eb, layout:androidx.recyclerview.widget.LinearLayoutManager@794e8a9, context:crc64126babe5824faebc.MainActivity@bf5663 [RecyclerView] at androidx.recyclerview.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:3451) ...

image