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

Commit cbd89a3

Browse files
authored
Fix crash adding items to CarouselView on Android (#13617) fixes #13616
1 parent 48e434b commit cbd89a3

File tree

4 files changed

+202
-5
lines changed

4 files changed

+202
-5
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<local:TestContentPage
3+
xmlns="http://xamarin.com/schemas/2014/forms"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
mc:Ignorable="d"
8+
Title="Test 13616" xmlns:local="using:Xamarin.Forms.Controls"
9+
x:Class="Xamarin.Forms.Controls.Issues.Issue13616">
10+
<StackLayout>
11+
<Label
12+
Padding="12"
13+
BackgroundColor="Black"
14+
TextColor="White"
15+
Text="Tap the Button to add a new Item, without exceptions, the test has passed."/>
16+
<StackLayout Padding="20,40,20,20">
17+
<Button
18+
AutomationId="AddItemButtonId"
19+
Text="Add new Item"
20+
CornerRadius="10"
21+
Command="{Binding AddCardCommand}"
22+
HeightRequest="40"
23+
WidthRequest="200"
24+
HorizontalOptions="CenterAndExpand"/>
25+
<CarouselView
26+
x:Name="carouselView"
27+
AutomationId="CarouselViewId"
28+
HorizontalScrollBarVisibility="Never"
29+
ItemsSource="{Binding Items}"
30+
PeekAreaInsets="15"
31+
Loop="False"
32+
HeightRequest="250"
33+
VerticalOptions="Start">
34+
<CarouselView.ItemsLayout>
35+
<LinearItemsLayout
36+
Orientation="Horizontal"
37+
SnapPointsAlignment="Center"
38+
SnapPointsType="MandatorySingle"
39+
ItemSpacing="6"/>
40+
</CarouselView.ItemsLayout>
41+
<CarouselView.ItemTemplate>
42+
<DataTemplate>
43+
<Frame
44+
HeightRequest="250"
45+
CornerRadius="10"
46+
HasShadow="False"
47+
Padding="0"
48+
IsClippedToBounds="True"
49+
Margin="10">
50+
<Grid
51+
BackgroundColor="{Binding Color}">
52+
<Grid Margin="20">
53+
<Grid.RowDefinitions>
54+
<RowDefinition Height="Auto" />
55+
<RowDefinition Height="*" />
56+
</Grid.RowDefinitions>
57+
<Label
58+
Grid.Row="0"
59+
Margin="0,0,0,20"
60+
Text="{Binding Name}"
61+
TextColor="Black" />
62+
<Label
63+
Grid.Row="1"
64+
Margin="0,0,0,20"
65+
Text="{Binding Desc}"
66+
TextColor="Black" />
67+
</Grid>
68+
</Grid>
69+
</Frame>
70+
</DataTemplate>
71+
</CarouselView.ItemTemplate>
72+
</CarouselView>
73+
</StackLayout>
74+
</StackLayout>
75+
</local:TestContentPage>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System.Collections.ObjectModel;
2+
using System.Linq;
3+
using System.Windows.Input;
4+
using Xamarin.Forms.CustomAttributes;
5+
using Xamarin.Forms.Internals;
6+
7+
#if UITEST
8+
using Xamarin.UITest;
9+
using NUnit.Framework;
10+
using Xamarin.Forms.Core.UITests;
11+
#endif
12+
13+
namespace Xamarin.Forms.Controls.Issues
14+
{
15+
#if UITEST
16+
[Category(UITestCategories.CarouselView)]
17+
#endif
18+
[Preserve(AllMembers = true)]
19+
[Issue(IssueTracker.Github, 13616,
20+
"[Bug] After updating XF 5.0.0.1931 getting Java.Lang.IllegalArgumentException: Invalid target position at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod",
21+
PlatformAffected.Android)]
22+
public partial class Issue13616 : TestContentPage
23+
{
24+
public Issue13616()
25+
{
26+
#if APP
27+
InitializeComponent();
28+
BindingContext = new Issue13616ViewModel();
29+
#endif
30+
}
31+
32+
protected override void Init()
33+
{
34+
}
35+
36+
#if UITEST && __ANDROID__
37+
[Test]
38+
public void Issue13616Test()
39+
{
40+
RunningApp.WaitForElement("AddItemButtonId");
41+
RunningApp.Tap("AddItemButtonId");
42+
RunningApp.WaitForElement("CarouselViewId");
43+
}
44+
#endif
45+
}
46+
47+
[Preserve(AllMembers = true)]
48+
public class Issue13616Model
49+
{
50+
public string Name { get; set; }
51+
public string Desc { get; set; }
52+
public Color Color { get; set; }
53+
public double Scale { get; set; }
54+
}
55+
56+
[Preserve(AllMembers = true)]
57+
public class Issue13616ViewModel : BindableObject
58+
{
59+
int _i = 4;
60+
ObservableCollection<Issue13616Model> _myList { get; set; } = new ObservableCollection<Issue13616Model>();
61+
62+
public Issue13616ViewModel()
63+
{
64+
Items = new ObservableCollection<Issue13616Model>
65+
{
66+
new Issue13616Model
67+
{
68+
Name = "Card 1",
69+
Desc = "Card Holder Name 1",
70+
Color = Color.Yellow
71+
},
72+
new Issue13616Model
73+
{
74+
Name = "Card 2",
75+
Desc = "Card Holder Name 2",
76+
Color = Color.Orange
77+
},
78+
new Issue13616Model
79+
{
80+
Name = "Card 3",
81+
Desc = "Card Holder Name 3",
82+
Color = Color.Red
83+
}
84+
};
85+
}
86+
87+
public ObservableCollection<Issue13616Model> Items
88+
{
89+
get
90+
{
91+
return _myList;
92+
}
93+
set
94+
{
95+
_myList = value;
96+
OnPropertyChanged(nameof(Items));
97+
}
98+
}
99+
100+
public ICommand AddCardCommand { get { return new Command(AddCard); } }
101+
102+
void AddCard()
103+
{
104+
var tempList = Items.ToList();
105+
tempList.Add(new Issue13616Model
106+
{
107+
Name = "Card " + _i,
108+
Desc = "Card Holder Name " + _i,
109+
Color = Color.Blue
110+
});
111+
Items = new ObservableCollection<Issue13616Model>(tempList);
112+
_i++;
113+
}
114+
}
115+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,7 @@
17141714
<Compile Include="$(MSBuildThisFileDirectory)Issue13436.xaml.cs" />
17151715
<Compile Include="$(MSBuildThisFileDirectory)Issue8701.cs" />
17161716
<Compile Include="$(MSBuildThisFileDirectory)Issue13390.cs" />
1717+
<Compile Include="$(MSBuildThisFileDirectory)Issue13616.xaml.cs" />
17171718
</ItemGroup>
17181719
<ItemGroup>
17191720
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
@@ -2116,6 +2117,9 @@
21162117
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13436.xaml">
21172118
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
21182119
</EmbeddedResource>
2120+
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13616.xaml">
2121+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
2122+
</EmbeddedResource>
21192123
</ItemGroup>
21202124
<ItemGroup>
21212125
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla27417Xaml.xaml">

Xamarin.Forms.Platform.Android/CollectionView/CarouselViewRenderer.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,15 @@ protected override void ScrollTo(ScrollToRequestEventArgs args)
205205

206206
if (_carouselViewLoopManager == null)
207207
return;
208-
//Special case here
209-
//We could have a race condition where we are scrolling our collection to center the first item
210-
//And at the same time the user is requesting we go to a particular item
211-
if (position == -1 && Carousel.Loop)
208+
209+
// Special case here
210+
// We could have a race condition where we are scrolling our collection to center the first item
211+
// And at the same time the user is requesting we go to a particular item
212+
if (position == -1)
212213
{
213-
_carouselViewLoopManager.AddPendingScrollTo(args);
214+
if (Carousel.Loop)
215+
_carouselViewLoopManager.AddPendingScrollTo(args);
216+
214217
return;
215218
}
216219

0 commit comments

Comments
 (0)