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

[Android] Listview delete item and SwipeView content parent #13439

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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,85 @@
<?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"
xmlns:local="using:Xamarin.Forms.Controls"
mc:Ignorable="d"
Title="Test 13399"
x:Name="Issue13399Page"
x:Class="Xamarin.Forms.Controls.Issues.Issue13399">
<Grid
RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="Delete items, without exceptions the test has passed."/>
<ScrollView
Grid.Row="1">
<StackLayout
VerticalOptions="Start">
<ListView
ItemsSource="{Binding Items}"
IsVisible="True"
HasUnevenRows="True"
SeparatorVisibility="Default">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<SwipeView>
<SwipeView.LeftItems>
<SwipeItems>
<SwipeItem
Text="Share"
BackgroundColor="#2196F3">
</SwipeItem>
<SwipeItem
Text="Edit"
BackgroundColor="Orange">
</SwipeItem>
</SwipeItems>
</SwipeView.LeftItems>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem
Text="Delete"
BackgroundColor="Red"
Command="{Binding Source={x:Reference Issue13399Page}, Path=BindingContext.DeleteItemCommand}"
CommandParameter="{Binding}"/>
</SwipeItems>
</SwipeView.RightItems>
<Grid>
<Frame>
<StackLayout>
<Grid>
<Label
Text="{Binding Text}"
TextColor="Black"
Margin="2"
FontSize="12"
HorizontalOptions="FillAndExpand"
VerticalOptions="StartAndExpand"/>
</Grid>
</StackLayout>
</Frame>
</Grid>
</SwipeView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ScrollView>
<Button
Grid.Row="2"
Text="Delete Item"
Command="{Binding DeleteLastItemCommand}"/>
</Grid>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Collections.ObjectModel;
using System.Windows.Input;

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

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 13399,
"[Bug] The specified child already has a parent",
PlatformAffected.Android)]
public partial class Issue13399 : TestContentPage
{
public Issue13399()
{
#if APP
InitializeComponent();
BindingContext = new Issue13399ViewModel();
#endif
}

protected override void Init()
{

}
}

[Preserve(AllMembers = true)]
public class Issue13399Model
{
public int Id { get; set; }
public string Text { get; set; }
}

[Preserve(AllMembers = true)]
public class Issue13399ViewModel : BindableObject
{
ObservableCollection<Issue13399Model> _items;

public Issue13399ViewModel()
{
Items = new ObservableCollection<Issue13399Model>();
LoadItems();
}

public ObservableCollection<Issue13399Model> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged();
}
}

public ICommand DeleteLastItemCommand => new Command(ExecuteDeleteLastItem);
public ICommand DeleteItemCommand => new Command<Issue13399Model>(ExecuteDeleteItem);

void LoadItems()
{
for (int i = 0; i < 100; i++)
{
Items.Add(new Issue13399Model { Id = i + 1, Text = $"Item {i + 1}" });
}
}

void ExecuteDeleteLastItem()
{
if (Items.Count == 0)
return;

var index = Items.Count - 1;
Items.RemoveAt(index);
}

void ExecuteDeleteItem(Issue13399Model item)
{
if (Items.Count == 0)
return;

Items.Remove(item);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutBackground.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentOffest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentWithZeroMargin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13399.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13337.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13232.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13378.xaml.cs" />
Expand Down Expand Up @@ -2223,6 +2224,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13136.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13399.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13337.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
14 changes: 6 additions & 8 deletions Xamarin.Forms.Platform.Android/Renderers/SwipeViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,15 @@ void PropagateParentTouch()

void UpdateContent()
{
if (_contentView != null)
{
_contentView.RemoveFromParent();
_contentView.Dispose();
_contentView = null;
}
RemoveAllViews();

if (Element.Content == null)
_contentView = CreateEmptyContent();
else
_contentView = CreateContent();
_contentView = GetOrCreateContent();

if (_contentView.Parent != null)
_contentView.RemoveFromParent();

AddView(_contentView);

Expand All @@ -408,7 +406,7 @@ AView CreateEmptyContent()
return emptyContentView;
}

AView CreateContent()
AView GetOrCreateContent()
{
var renderer = Element.Content.GetRenderer() ?? Platform.CreateRendererWithContext(Element.Content, Context);
Platform.SetRenderer(Element.Content, renderer);
Expand Down