Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[Android] Fix occasional wrong touch interception in SwipeView Content (
Browse files Browse the repository at this point in the history
#13732)

* Fix wrong touch interception in SwipeView Content on Android

* Updated swipe delta
  • Loading branch information
jsuarezruiz committed Sep 6, 2021
1 parent 4f1a837 commit 3470b92
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test 13726" xmlns:local="using:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue13726">
<StackLayout>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="Tap several times the Button. If the counter is always updated, the test has passed."/>
<StackLayout
Padding="12">
<Label
Text="Button"/>
<Button
Text="Tap"
Clicked="OnButtonClicked"/>
<Label
x:Name="ButtonInfoLabel"
FontSize="Medium"
HorizontalOptions="Center"/>
<Label
Text="Button inside SwipeView"/>
<SwipeView>
<SwipeView.RightItems>
<SwipeItem
Text="Delete"
BackgroundColor="Red"/>
</SwipeView.RightItems>
<Grid
BackgroundColor="LightGray">
<Button
AutomationId="SwipeViewButtonId"
Text="Tap"
Clicked="OnSwipeViewButtonClicked"/>
</Grid>
</SwipeView>
<Label
x:Name="SwipeViewInfoLabel"
AutomationId="SwipeViewInfoLabelId"
FontSize="Medium"
HorizontalOptions="Center"/>
</StackLayout>
</StackLayout>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms.CustomAttributes;
using System.Threading.Tasks;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Issue(IssueTracker.Github, 13726,
"[Bug] Clicks on elements within a SwipeView are buggy",
PlatformAffected.Android)]
public partial class Issue13726 : TestContentPage
{
#if APP
int _buttonCounter = 0;
int _swipeViewCounter = 0;
#endif

public Issue13726()
{
#if APP
InitializeComponent();
#endif
}

protected override void Init()
{
}

#if APP
void OnButtonClicked(object sender, EventArgs e)
{
_buttonCounter++;
ButtonInfoLabel.Text = $"SwipeView Button tapped {_buttonCounter} times";
}

void OnSwipeViewButtonClicked(object sender, EventArgs e)
{
_swipeViewCounter++;
SwipeViewInfoLabel.Text = $"SwipeView Button tapped {_swipeViewCounter} times";
}
#endif

#if UITEST && __ANDROID__
[Test]
public void TouchSwipeViewContentTest()
{
string swipeViewButtonId = "SwipeViewButtonId";
string swipeViewInfoLabelId = "SwipeViewInfoLabelId";

RunningApp.WaitForElement(q => q.Marked(swipeViewButtonId));

for (int i = 0; i < 10; i++)
RunningApp.Tap(q => q.Marked(swipeViewButtonId));

RunningApp.SwipeRightToLeft(q => q.Marked(swipeViewButtonId));

for (int i = 0; i < 10; i++)
RunningApp.Tap(q => q.Marked(swipeViewButtonId));

var infoLabel = RunningApp.WaitForFirstElement(swipeViewInfoLabelId);

Assert.AreEqual("SwipeView Button tapped 20 times", infoLabel.ReadText());
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue13684.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12300.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12150.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13726.xaml.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
Expand Down Expand Up @@ -2195,6 +2196,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13684.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13726.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla27417Xaml.xaml">
Expand Down
28 changes: 19 additions & 9 deletions Xamarin.Forms.Platform.Android/Renderers/SwipeViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,31 @@ bool ShouldInterceptTouch(MotionEvent e)
var interceptPoint = new Point(e.GetX() / _density, e.GetY() / _density);

var diffX = interceptPoint.X - _initialPoint.X;
var aDiffX = Math.Abs(diffX);

var diffY = interceptPoint.Y - _initialPoint.Y;
var aDiffY = Math.Abs(diffY);

SwipeDirection swipeDirection;
var swipeMinimumDelta = 1.0f;

if (Math.Abs(diffX) > Math.Abs(diffY))
swipeDirection = diffX > 0 ? SwipeDirection.Right : SwipeDirection.Left;
else
swipeDirection = diffY > 0 ? SwipeDirection.Down : SwipeDirection.Up;
if (aDiffX >= swipeMinimumDelta || aDiffY >= swipeMinimumDelta)
{
SwipeDirection swipeDirection;

var items = GetSwipeItemsByDirection(swipeDirection);
if (aDiffX > aDiffY)
swipeDirection = diffX > 0 ? SwipeDirection.Right : SwipeDirection.Left;
else
swipeDirection = diffY > 0 ? SwipeDirection.Down : SwipeDirection.Up;

if (items == null || items.Count == 0)
return false;
var items = GetSwipeItemsByDirection(swipeDirection);

if (items == null || items.Count == 0)
return false;

return ShouldInterceptScrollChildrenTouch(swipeDirection);
return ShouldInterceptScrollChildrenTouch(swipeDirection);
}

return false;
}

bool ShouldInterceptScrollChildrenTouch(SwipeDirection swipeDirection)
Expand Down

0 comments on commit 3470b92

Please sign in to comment.