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

Commit

Permalink
[Android] Fix swipe conflict between SwipeView and TabbedPage (#11358)
Browse files Browse the repository at this point in the history
* Added repro sample

* Updated sample

* Added instructions to the sample

* Fix the issue

Co-authored-by: Samantha Houts <samhouts@users.noreply.github.com>

fixes #11314
  • Loading branch information
jsuarezruiz committed Aug 17, 2020
1 parent 9a9b842 commit e966d64
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

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

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.SwipeView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 11314,
"Cannot swipe the swipeview inside a Listview[Bug]",
PlatformAffected.Android)]
public class Issue11314 : TestTabbedPage
{
public Issue11314()
{
#if APP
Device.SetFlags(new List<string> { ExperimentalFlags.SwipeViewExperimental });
#endif
}

protected override void Init()
{
Title = "Issue 11314";

var page1 = new Issue11314Page("Page 1");
var page2 = new Issue11314Page("Page 2");

Children.Add(page1);
Children.Add(page2);
}
}

public class Issue11314Page : ContentPage
{
public Issue11314Page(string title)
{
Title = title;

for (int i = 0; i < 5; i++)
{
Items.Add(DateTime.Now.ToString());
}

var layout = new StackLayout
{
Padding = 0
};

var instructions = new Label
{
Padding = 12,
BackgroundColor = Color.Black,
TextColor = Color.White,
Text = "Try to open the SwipeView doing swipe, if you can open it, the test has passed."
};

var listView = new ListView()
{
HorizontalOptions = LayoutOptions.FillAndExpand,
HasUnevenRows = true,
ItemsSource = Items,
ItemTemplate = new DataTemplate(() =>
{
var itemLayout = new StackLayout();
var swipeView = new SwipeView();
var content = new Grid
{
HeightRequest = 80,
BackgroundColor = Color.LightGray
};
content.Children.Add(new Label
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Text = "Swipe to Left"
});
swipeView.Content = content;
var swipeItem = new SwipeItem
{
BackgroundColor = Color.Red,
Text = "Text"
};
swipeView.RightItems = new SwipeItems
{
swipeItem
};
itemLayout.Children.Add(swipeView);
return new ViewCell { View = itemLayout };
})
};

layout.Children.Add(instructions);
layout.Children.Add(listView);

Content = layout;
}

public ObservableCollection<string> Items = new ObservableCollection<string>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue11291.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11244.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11272.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11314.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11413.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11563.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11547.xaml.cs" />
Expand Down
14 changes: 14 additions & 0 deletions Xamarin.Forms.Platform.Android/Renderers/SwipeViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#endif
using Android.Views;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.Android.AppCompat;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using APointF = Android.Graphics.PointF;
using ATextAlignment = Android.Views.TextAlignment;
Expand All @@ -34,6 +35,7 @@ public class SwipeViewRenderer : ViewRenderer<SwipeView, AView>
readonly Dictionary<ISwipeItem, object> _swipeItems;
readonly Context _context;
View _scrollParent;
FormsViewPager _viewPagerParent;
AView _contentView;
LinearLayoutCompat _actionView;
SwipeTransitionMode _swipeTransitionMode;
Expand Down Expand Up @@ -173,6 +175,9 @@ protected override void OnAttachedToWindow()
{
base.OnAttachedToWindow();

if (Control != null && Control.Parent != null && _viewPagerParent == null)
_viewPagerParent = Control.Parent.GetParentOfType<FormsViewPager>();

if (Element != null && _scrollParent == null)
{
_scrollParent = Element.FindParentOfType<ScrollView>();
Expand Down Expand Up @@ -506,6 +511,7 @@ bool ProcessTouchMove(APointF point)
if (!ValidateSwipeDirection() || _isResettingSwipe)
return false;

EnableParentGesture(false);
_swipeOffset = GetSwipeOffset(_initialPoint, point);
UpdateIsOpen(_swipeOffset != 0);

Expand All @@ -523,6 +529,8 @@ bool ProcessTouchUp()
{
_isTouchDown = false;

EnableParentGesture(true);

if (!_isSwiping)
return false;

Expand Down Expand Up @@ -1321,6 +1329,12 @@ void ExecuteSwipeItem(ISwipeItem item)
item.OnInvoked();
}

void EnableParentGesture(bool isGestureEnabled)
{
if (_viewPagerParent != null)
_viewPagerParent.EnableGesture = isGestureEnabled;
}

void OnOpenRequested(object sender, OpenSwipeEventArgs e)
{
if (_contentView == null)
Expand Down

0 comments on commit e966d64

Please sign in to comment.