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

Commit aa71924

Browse files
authored
[Android] Fix SwipeView with gestures issue (#11834)
* Close SwipeView tapping the content and using gestures * Updated the test instructions * spaces Co-authored-by: Samantha Houts <[email protected]> fixes #11496
1 parent bde715b commit aa71924

File tree

4 files changed

+244
-9
lines changed

4 files changed

+244
-9
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<controls:TestContentPage
3+
xmlns:controls="clr-namespace:Xamarin.Forms.Controls"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:issues="clr-namespace:Xamarin.Forms.Controls.Issues"
9+
mc:Ignorable="d"
10+
x:Class="Xamarin.Forms.Controls.Issues.Issue11496"
11+
x:Name="Issue11496Page"
12+
Title="Issue 11496">
13+
<controls:TestContentPage.BindingContext>
14+
<issues:Issue11496ViewModel />
15+
</controls:TestContentPage.BindingContext>
16+
<StackLayout
17+
Padding="0">
18+
<Label
19+
Padding="12"
20+
BackgroundColor="Black"
21+
TextColor="White"
22+
Text="If tapping an item appear a dialog, the test has passed."/>
23+
<StackLayout
24+
Orientation="Vertical"
25+
BindableLayout.ItemsSource="{Binding Items}">
26+
<BindableLayout.ItemTemplate>
27+
<DataTemplate>
28+
<SwipeView>
29+
<SwipeView.RightItems>
30+
<SwipeItems>
31+
<SwipeItemView>
32+
<StackLayout>
33+
<Button
34+
Text="Delete"
35+
VerticalOptions="Fill"/>
36+
</StackLayout>
37+
</SwipeItemView>
38+
</SwipeItems>
39+
</SwipeView.RightItems>
40+
<StackLayout
41+
HeightRequest="60"
42+
BackgroundColor="LightGray">
43+
<issues:Issue11496ItemControl
44+
TestItem="{Binding}"
45+
HorizontalOptions="FillAndExpand"
46+
VerticalOptions="FillAndExpand"/>
47+
</StackLayout>
48+
</SwipeView>
49+
</DataTemplate>
50+
</BindableLayout.ItemTemplate>
51+
</StackLayout>
52+
</StackLayout>
53+
</controls:TestContentPage>
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
using Xamarin.Forms.CustomAttributes;
2+
using Xamarin.Forms.Internals;
3+
using Xamarin.Forms.Xaml;
4+
using System.Collections.Generic;
5+
using System.Windows.Input;
6+
7+
#if UITEST
8+
using Xamarin.UITest;
9+
using Xamarin.UITest.Queries;
10+
using NUnit.Framework;
11+
using Xamarin.Forms.Core.UITests;
12+
#endif
13+
14+
namespace Xamarin.Forms.Controls.Issues
15+
{
16+
#if UITEST
17+
[NUnit.Framework.Category(UITestCategories.SwipeView)]
18+
#endif
19+
#if APP
20+
[XamlCompilation(XamlCompilationOptions.Compile)]
21+
#endif
22+
[Preserve(AllMembers = true)]
23+
[Issue(IssueTracker.Github, 11496, "[Bug] Issue with SwipeView not working since Xamarin.Forms update v4.7.0.1080 and above on Android",
24+
PlatformAffected.Android)]
25+
public partial class Issue11496 : TestContentPage
26+
{
27+
public Issue11496()
28+
{
29+
#if APP
30+
Title = "Issue 11496";
31+
Device.SetFlags(new List<string> { ExperimentalFlags.SwipeViewExperimental });
32+
InitializeComponent();
33+
#endif
34+
}
35+
36+
protected override void Init()
37+
{
38+
39+
}
40+
}
41+
42+
[Preserve(AllMembers = true)]
43+
public partial class Issue11496ItemControl : ContentView
44+
{
45+
readonly Label _label;
46+
47+
public Issue11496ItemControl()
48+
{
49+
var layout = new Grid();
50+
51+
_label = new Label
52+
{
53+
HorizontalOptions = LayoutOptions.Center,
54+
VerticalOptions = LayoutOptions.Center
55+
};
56+
57+
layout.Children.Add(_label);
58+
59+
Content = layout;
60+
61+
var tapCommand = new TapGestureRecognizer
62+
{
63+
Command = Command,
64+
CommandParameter = CommandParameter
65+
};
66+
67+
layout.GestureRecognizers.Add(tapCommand);
68+
69+
tapCommand.Tapped += (sender, args) =>
70+
{
71+
if (TestItem != null)
72+
Application.Current.MainPage.DisplayAlert("Issue11496", $"Tapped {((Issue11496Item)TestItem).Name}", "Ok");
73+
};
74+
}
75+
76+
public static readonly BindableProperty TestItemProperty =
77+
BindableProperty.Create(nameof(TestItem), typeof(object), typeof(Issue11496ItemControl));
78+
79+
public static readonly BindableProperty CommandProperty =
80+
BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(Issue11496ItemControl));
81+
82+
public static readonly BindableProperty CommandParameterProperty =
83+
BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(Issue11496ItemControl));
84+
85+
public object TestItem
86+
{
87+
get => GetValue(TestItemProperty);
88+
set => SetValue(TestItemProperty, value);
89+
}
90+
91+
public ICommand Command
92+
{
93+
get => (ICommand)GetValue(CommandProperty);
94+
set => SetValue(CommandProperty, value);
95+
}
96+
97+
public object CommandParameter
98+
{
99+
get => GetValue(CommandParameterProperty);
100+
set => SetValue(CommandParameterProperty, value);
101+
}
102+
103+
protected override void OnBindingContextChanged()
104+
{
105+
base.OnBindingContextChanged();
106+
107+
if (TestItem != null)
108+
_label.Text = ((Issue11496Item)TestItem).Name;
109+
110+
}
111+
}
112+
113+
[Preserve(AllMembers = true)]
114+
public class Issue11496Item
115+
{
116+
public string Name { get; set; }
117+
}
118+
119+
[Preserve(AllMembers = true)]
120+
public class Issue11496ViewModel : BindableObject
121+
{
122+
List<Issue11496Item> _items;
123+
ICommand _command;
124+
object _commandParam;
125+
126+
public List<Issue11496Item> Items
127+
{
128+
get => _items;
129+
set
130+
{
131+
_items = value;
132+
OnPropertyChanged();
133+
}
134+
}
135+
136+
public ICommand Command
137+
{
138+
get => _command;
139+
set
140+
{
141+
_command = value;
142+
OnPropertyChanged();
143+
}
144+
}
145+
146+
public object CommandParameter
147+
{
148+
get => _commandParam;
149+
set
150+
{
151+
_commandParam = value;
152+
OnPropertyChanged();
153+
}
154+
}
155+
156+
public Issue11496ViewModel()
157+
{
158+
Items = new List<Issue11496Item>()
159+
{
160+
new Issue11496Item() { Name = "Test One" },
161+
new Issue11496Item() { Name = "Test Two" },
162+
new Issue11496Item() { Name = "Test Three" },
163+
new Issue11496Item() { Name = "Test Four" },
164+
new Issue11496Item() { Name = "Test Five" },
165+
new Issue11496Item() { Name = "Test Six" },
166+
new Issue11496Item() { Name = "Test Seven" },
167+
new Issue11496Item() { Name = "Test Eight" },
168+
};
169+
}
170+
}
171+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,6 @@
13851385
<Compile Include="$(MSBuildThisFileDirectory)Issue9734.xaml.cs" />
13861386
<Compile Include="$(MSBuildThisFileDirectory)Issue8767.xaml.cs" />
13871387
<Compile Include="$(MSBuildThisFileDirectory)Issue8778.xaml.cs" />
1388-
<Compile Include="$(MSBuildThisFileDirectory)Issue9088.cs" />
13891388
<Compile Include="$(MSBuildThisFileDirectory)Issue9646.xaml.cs" />
13901389
<Compile Include="$(MSBuildThisFileDirectory)Issue9735.xaml.cs" />
13911390
<Compile Include="$(MSBuildThisFileDirectory)Issue9305.xaml.cs" />
@@ -1478,6 +1477,10 @@
14781477
<Compile Include="$(MSBuildThisFileDirectory)Issue11653.xaml.cs" />
14791478
<Compile Include="$(MSBuildThisFileDirectory)Issue11869.cs" />
14801479
<Compile Include="$(MSBuildThisFileDirectory)Issue11723.cs" />
1480+
<Compile Include="$(MSBuildThisFileDirectory)Issue11737.xaml.cs" />
1481+
<Compile Include="$(MSBuildThisFileDirectory)Issue11764.xaml.cs" />
1482+
<Compile Include="$(MSBuildThisFileDirectory)Issue11573.xaml.cs" />
1483+
<Compile Include="$(MSBuildThisFileDirectory)Issue11496.xaml.cs" />
14811484
</ItemGroup>
14821485
<ItemGroup>
14831486
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
@@ -1750,6 +1753,9 @@
17501753
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue11653.xaml">
17511754
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
17521755
</EmbeddedResource>
1756+
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue11496.xaml">
1757+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
1758+
</EmbeddedResource>
17531759
</ItemGroup>
17541760
<ItemGroup>
17551761
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla27417Xaml.xaml">

Xamarin.Forms.Platform.Android/Renderers/SwipeViewRenderer.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,16 @@ bool ShouldInterceptTouch(MotionEvent e)
275275

276276
SwipeDirection swipeDirection;
277277

278-
if(Math.Abs(diffX) > Math.Abs(diffY))
278+
if (Math.Abs(diffX) > Math.Abs(diffY))
279279
swipeDirection = diffX > 0 ? SwipeDirection.Right : SwipeDirection.Left;
280280
else
281281
swipeDirection = diffY > 0 ? SwipeDirection.Down : SwipeDirection.Up;
282-
282+
283283
var items = GetSwipeItemsByDirection(swipeDirection);
284284

285285
if (items == null || items.Count == 0)
286286
return false;
287-
287+
288288
return true;
289289
}
290290

@@ -296,8 +296,8 @@ public override bool OnInterceptTouchEvent(MotionEvent e)
296296
public override bool DispatchTouchEvent(MotionEvent e)
297297
{
298298
if (e.Action == MotionEventActions.Down)
299-
{
300-
_downX = e.RawX;
299+
{
300+
_downX = e.RawX;
301301
_downY = e.RawY;
302302
_initialPoint = new APointF(e.GetX() / _density, e.GetY() / _density);
303303
}
@@ -309,8 +309,13 @@ public override bool DispatchTouchEvent(MotionEvent e)
309309
if (CanProcessTouchSwipeItems(touchUpPoint))
310310
ProcessTouchSwipeItems(touchUpPoint);
311311
else
312+
{
313+
if (!_isSwiping && _isOpen && TouchInsideContent(touchUpPoint))
314+
ResetSwipe();
315+
312316
PropagateParentTouch();
313-
}
317+
}
318+
}
314319

315320
return base.DispatchTouchEvent(e);
316321
}
@@ -325,7 +330,7 @@ void PropagateParentTouch()
325330
if (itemContentView != null && !((ISwipeViewController)Element).IsOpen)
326331
itemContentView.ClickOn();
327332
}
328-
333+
329334
void UpdateContent()
330335
{
331336
if (Element.Content == null)
@@ -1365,7 +1370,7 @@ void ProgrammaticallyOpenSwipeItem(OpenSwipeItem openSwipeItem)
13651370

13661371
void UpdateIsOpen(bool isOpen)
13671372
{
1368-
if (Element == null)
1373+
if (Element == null)
13691374
return;
13701375

13711376
((ISwipeViewController)Element).IsOpen = isOpen;

0 commit comments

Comments
 (0)