Skip to content

Commit

Permalink
Fix Incorrect First and Last Visible Item index in CollectionView Scr…
Browse files Browse the repository at this point in the history
…olled Event (#26468)

* Fixed the CollectionView Scrolled Calls and parameters are incorrect

* Removed unwanted changes

* Fix the failures cases

* Fixed the failed scenarios

* Removed the unwanted  code changes

* Removed unwanted line

* Modified a test case

* Modified a test cases

* Added a details for mac failure

---------

Co-authored-by: Shalini-Ashokan <[email protected]>
  • Loading branch information
1 parent 4c42078 commit b5d4c66
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,63 @@ protected virtual (int First, int Center, int Last) GetVisibleItemsIndex(Recycle
lastVisibleItemIndex = linearLayoutManager.FindLastVisibleItemPosition();
centerItemIndex = recyclerView.CalculateCenterItemIndex(firstVisibleItemIndex, linearLayoutManager, _getCenteredItemOnXAndY);
}

bool hasHeader = ItemsViewAdapter.ItemsSource.HasHeader;
bool hasFooter = ItemsViewAdapter.ItemsSource.HasFooter;
int itemsCount = ItemsViewAdapter.ItemCount;

if (!hasHeader && !hasFooter)
{
return (firstVisibleItemIndex, centerItemIndex, lastVisibleItemIndex);
}

if (firstVisibleItemIndex == 0 && lastVisibleItemIndex == itemsCount - 1)
{
lastVisibleItemIndex -= hasHeader && hasFooter ? 2 : 1;
}
else
{
if (hasHeader && !hasFooter)
{
lastVisibleItemIndex -= 1;
firstVisibleItemIndex -= 1;
}
else if (!hasHeader && hasFooter)
{
if (lastVisibleItemIndex == itemsCount - 1)
{
lastVisibleItemIndex -= 1;
}
}
else if (hasHeader && hasFooter)
{
if (firstVisibleItemIndex == 0)
{
lastVisibleItemIndex -= 1;
}
else if (lastVisibleItemIndex != itemsCount - 1)
{
firstVisibleItemIndex -= 1;
lastVisibleItemIndex -= 1;
}
else
{
firstVisibleItemIndex -= 1;
lastVisibleItemIndex -= 2;
}
}
}

if (firstVisibleItemIndex < 0)
{
firstVisibleItemIndex = 0;
}

if (lastVisibleItemIndex < 0)
{
lastVisibleItemIndex = 0;
}

return (firstVisibleItemIndex, centerItemIndex, lastVisibleItemIndex);
}

Expand Down
66 changes: 66 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25649.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue25649"
xmlns:local="clr-namespace:Maui.Controls.Sample.Issues">

<ContentPage.BindingContext>
<local:_25649MainViewModel/>
</ContentPage.BindingContext>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.8*"/>
<RowDefinition Height="0.2*"/>
</Grid.RowDefinitions>

<CollectionView AutomationId="collectionView"
x:DataType="local:_25649MainViewModel"
ItemsSource="{Binding _25649People}"
Scrolled="OnCollectionViewScrolled"
BackgroundColor="DarkGray">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical"
SnapPointsType="Mandatory"/>
</CollectionView.ItemsLayout>

<CollectionView.Header>
<StackLayout BackgroundColor="LightGray">
<Label Margin="10,0,0,0"
Text="People"
FontSize="12"
FontAttributes="Bold"/>
</StackLayout>
</CollectionView.Header>

<CollectionView.ItemTemplate>
<DataTemplate x:DataType="local:_25649Person">
<VerticalStackLayout>
<Label Text="{Binding Name}"
FontSize="Medium"/>
<Rectangle HeightRequest="2"
BackgroundColor="Black"/>
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>

<CollectionView.Footer>
<StackLayout BackgroundColor="LightGray">
<Label Margin="10,0,0,0"
Text="Footer"
FontSize="12"
FontAttributes="Bold"/>
</StackLayout>
</CollectionView.Footer>
</CollectionView>

<Label
x:Name="lastVisibleItemIndex"
Grid.Row="1"
AutomationId="LastVisibleItemIndex"
Text="Last Visible Item Index"
HorizontalOptions="Start"
VerticalOptions="Center"/>

</Grid>
</ContentPage>
71 changes: 71 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25649.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows.Input;

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 25649, "CollectionView OnCollectionViewScrolled Calls and parameters are inconsistent or incorrect", PlatformAffected.All)]
public partial class Issue25649 : ContentPage
{
public Issue25649()
{
InitializeComponent();
}

void OnCollectionViewScrolled(object sender, ItemsViewScrolledEventArgs e)
{
lastVisibleItemIndex.Text = e.LastVisibleItemIndex.ToString();
}
}

public class _25649MainViewModel : BindableObject
{
public ObservableCollection<_25649Person> _25649People { get; set; }
public _25649MainViewModel()
{

_25649People = new ObservableCollection<_25649Person>()
{
new _25649Person() { Name = "Person 0" },
new _25649Person() { Name = "Person 1" },
new _25649Person() { Name = "Person 2" },
new _25649Person() { Name = "Person 3" },
new _25649Person() { Name = "Person 4" },
new _25649Person() { Name = "Person 5" },
new _25649Person() { Name = "Person 6" },
new _25649Person() { Name = "Person 7" },
new _25649Person() { Name = "Person 8" },
new _25649Person() { Name = "Person 9" },
new _25649Person() { Name = "Person 10" },
new _25649Person() { Name = "Person 11" },
new _25649Person() { Name = "Person 12" },
new _25649Person() { Name = "Person 13" },
new _25649Person() { Name = "Person 14" },
new _25649Person() { Name = "Person 15" },
new _25649Person() { Name = "Person 16" },
new _25649Person() { Name = "Person 17" },
new _25649Person() { Name = "Person 18" },
new _25649Person() { Name = "Person 19" },
new _25649Person() { Name = "Person 20" },
new _25649Person() { Name = "Person 21" },
new _25649Person() { Name = "Person 22" },
new _25649Person() { Name = "Person 23" },
new _25649Person() { Name = "Person 24" },
new _25649Person() { Name = "Person 25" },
new _25649Person() { Name = "Person 26" },
new _25649Person() { Name = "Person 27" },
new _25649Person() { Name = "Person 28" },
new _25649Person() { Name = "Person 29" },
new _25649Person() { Name = "Person 30" },
};
}
}

public class _25649Person
{
public string Name { get; set; }
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#if TEST_FAILS_ON_CATALYST // Scroll actions do not work on MacCatalyst, so this test is ignored on MacCatalyst.

using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue25649 : _IssuesUITest
{
public Issue25649(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "CollectionView OnCollectionViewScrolled Calls and parameters are inconsistent or incorrect";

[Test]
[Category(UITestCategories.CollectionView)]
public void Issue25649Test()
{
App.WaitForElement("collectionView");
App.ScrollDown("collectionView", ScrollStrategy.Gesture, 0.99);

// The ScrollDown method returns a different FirstVisibleItemIndex values on each call, so it cannot be used for consistent test validation.

var lastVisibleItemIndex = App.FindElement("LastVisibleItemIndex").GetText();
Assert.That(lastVisibleItemIndex, Is.EqualTo("30"));
}
}
}
#endif

0 comments on commit b5d4c66

Please sign in to comment.