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

[Android] Fix swipe conflict between SwipeView and TabbedPage #11358

Merged
merged 9 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1446,6 +1446,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue11120.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11291.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11272.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11314.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
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 @@ -160,6 +162,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 @@ -493,6 +498,7 @@ bool ProcessTouchMove(APointF point)
if (!ValidateSwipeDirection() || _isResettingSwipe)
return false;

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

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

EnableParentGesture(true);

if (!_isSwiping)
return false;

Expand Down Expand Up @@ -1308,6 +1316,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