diff --git a/src/Controls/src/Core/Platform/Android/TapAndPanGestureDetector.cs b/src/Controls/src/Core/Platform/Android/TapAndPanGestureDetector.cs index 03bb05bc1fbc..d9775513ec31 100644 --- a/src/Controls/src/Core/Platform/Android/TapAndPanGestureDetector.cs +++ b/src/Controls/src/Core/Platform/Android/TapAndPanGestureDetector.cs @@ -40,8 +40,7 @@ public void UpdateLongPressSettings() public override bool OnTouchEvent(MotionEvent ev) { - if (base.OnTouchEvent(ev)) - return true; + var handled = base.OnTouchEvent(ev); if (_pointerGestureHandler != null && ev?.Action is MotionEventActions.Up or MotionEventActions.Down or MotionEventActions.Cancel) @@ -49,10 +48,13 @@ public override bool OnTouchEvent(MotionEvent ev) _pointerGestureHandler.OnTouch(ev); } + // Always call EndScrolling on ACTION_UP to ensure swipe gestures are completed, + // regardless of whether the base gesture detector consumed the event. + // This fixes SwipeGestureRecognizer not working on scrollable views like CollectionView. if (_listener != null && ev?.Action == MotionEventActions.Up) _listener.EndScrolling(); - return false; + return handled; } protected override void Dispose(bool disposing) diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue16624.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue16624.xaml new file mode 100644 index 000000000000..2ab605e6bbeb --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue16624.xaml @@ -0,0 +1,41 @@ + + + + + diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue16624.xaml.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue16624.xaml.cs new file mode 100644 index 000000000000..da8c90950fd6 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue16624.xaml.cs @@ -0,0 +1,15 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 16624, "SwipeGesture is not working on a ListView/CollectionView", PlatformAffected.Android)] +public partial class Issue16624 : ContentPage +{ + public Issue16624() + { + InitializeComponent(); + } + + private void SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e) + { + StatusLabel.Text = $"Swiped {e.Direction}"; + } +} diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue16624.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue16624.cs new file mode 100644 index 000000000000..3f5cbe106a5a --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue16624.cs @@ -0,0 +1,71 @@ +#if ANDROID +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues +{ + public class Issue16624 : _IssuesUITest + { + public Issue16624(TestDevice testDevice) : base(testDevice) + { + } + + public override string Issue => "SwipeGesture is not working on a ListView/CollectionView"; + + [Test] + [Category(UITestCategories.Gestures)] + public void SwipeGestureOnCollectionViewShouldWork() + { + // Wait for the page to load + App.WaitForElement("TestCollectionView"); + App.WaitForElement("StatusLabel"); + + // Get the CollectionView element to perform swipe on + var collectionView = App.WaitForElement("TestCollectionView"); + var rect = collectionView.GetRect(); + var centerX = rect.X + rect.Width / 2; + var centerY = rect.Y + rect.Height / 2; + + // Perform a left swipe on the CollectionView + App.DragCoordinates(centerX + 100, centerY, centerX - 100, centerY); + + // Wait for the status label to update + App.WaitForElement("StatusLabel"); + + // Verify the swipe was detected + var statusLabel = App.WaitForElement("StatusLabel"); + var labelText = statusLabel.GetText(); + + Assert.That(labelText, Does.Contain("Swiped"), "Swipe gesture should be detected on CollectionView"); + } + + [Test] + [Category(UITestCategories.Gestures)] + public void SwipeRightGestureOnCollectionViewShouldWork() + { + // Wait for the page to load + App.WaitForElement("TestCollectionView"); + App.WaitForElement("StatusLabel"); + + // Get the CollectionView element to perform swipe on + var collectionView = App.WaitForElement("TestCollectionView"); + var rect = collectionView.GetRect(); + var centerX = rect.X + rect.Width / 2; + var centerY = rect.Y + rect.Height / 2; + + // Perform a right swipe on the CollectionView + App.DragCoordinates(centerX - 100, centerY, centerX + 100, centerY); + + // Wait for the status label to update + App.WaitForElement("StatusLabel"); + + // Verify the swipe was detected + var statusLabel = App.WaitForElement("StatusLabel"); + var labelText = statusLabel.GetText(); + + Assert.That(labelText, Does.Contain("Swiped"), "Swipe gesture should be detected on CollectionView"); + } + } +} +#endif