Repository: dotnet/maui

Issue #25256: ListView ScrolledEventArgs.ScrollY does not reset when ItemsSource changes
Created: 2024-10-14T18:21:46Z
Description
When changing the ItemsSource of a ListView on Android, the ScrollY property does not reset properly to zero. Instead it remains on its current value. When scrolling after that, the ScrollY property changes according to the scrolled delta, but since it never properly reset before the value is always shifted and potentially never reaches zero again.
Steps to Reproduce

Create a ListView binding to a large set of items and bind to the Scrolled-Event.
Scroll down (must be so far that you surpass the length of the next ItemsSource you bind to (in the example repo scroll further than the first 100 items))
Via a button click, bind the ListView to a smaller set of items
The items are replaced, but the ListView is still at the position you've scrolled to before (Contrary to windows, where the ListView is automatically placed at the first item, when changing the ItemsSource)
When examine the ScrollY property of the ScrolledEventArgs, you should see that it is still set to some value greater than zero (depending on how far you've scrolled, on Windows ScrollY is properly reset to zero)
Scroll all the way to the top
When examining the ScrollY property of the ScrolledEventArgs again, you should see that it is now lower than before, but still greater then zero

(See the repo where I build this as an example)
Link to public reproduction project repository
https://github.com/Mano176/MauiAndroidScrollViewBug
Version with bug
Unknown/Other
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
Tested on Android 14
Did you find any workaround?
No response
Relevant log output
output of "donet workload list"
Installierte Workload-ID      Manifestversion        Installationsquelle
maccatalyst                   18.0.8303/8.0.100      VS 17.11.35327.3
ios                           18.0.8303/8.0.100      VS 17.11.35327.3
android                       34.0.113/8.0.100       VS 17.11.35327.3
maui-windows                  8.0.82/8.0.100         VS 17.11.35327.3
(Btw. why does none of the dropdown options under "Version with bug" seem to match this when filing a new issue?)

Duplicate issue #26661: [Android] ListView scroll position maintains after items refresh
Created: 2024-12-17T04:03:58Z
Description
When refreshing items in a ListView by clearing and adding new items, the scroll position is unexpectedly maintained on Android. On iOS and other platforms, the scroll position correctly resets to the top as expected.
Note: While fixing this issue, enable the Bugzilla31114 test cases.
Output

  
    
    

    Screen.Recording.2024-12-17.at.9.29.58.AM.mov
    
  

  

  


Code Snippet
using System.Collections.ObjectModel;

namespace MauiApp1;

public partial class MainPage : ContentPage
{
    private ObservableCollection<string> _items;
    private ListView _listView;
    private Button _btnLoad;

    public MainPage()
    {
        InitializeComponent();
        Init();
    }

    private void Init()
    {
        _items = new ObservableCollection<string>();

        _listView = new ListView
        {
            ItemsSource = _items,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        _btnLoad = new Button
        {
            Text = "Load Items",
            AutomationId = "btnLoad"
        };
        _btnLoad.Clicked += OnLoadButtonClicked;

        Content = new StackLayout
        {
            Children = { _listView, _btnLoad }
        };
    }

    private void OnLoadButtonClicked(object sender, EventArgs e)
    {
        LoadItems();
    }

    private void LoadItems()
    {
        _items.Clear();

        for (int i = 0; i < 20; i++)
        {
            _items.Add($"Item {i + 1}");
        }
    }
}

Steps to Reproduce

Run the attached code sample
Tap the "Load Items" button.
Scroll down the ListView.
Again Tap the "Load Items" button.
Observe the scroll position behavior on different platforms.

Expected Behavior:
The ListView should reset its scroll position to the top across all platforms when items are refreshed (cleared and new items added).
Actual Behavior:

On Android: The ListView unexpectedly maintains its scroll position after refreshing items.
On iOS and other platforms: The ListView correctly resets its scroll position to the top after refreshing items.

Link to public reproduction project repository
No response
Version with bug
9.0.21 SR2.1
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 11
Did you find any workaround?
NA
Relevant log output

Closing comment (2024-12-17T13:48:13Z): Probably a duplicate of: #25256