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

[Android] Fix set CurrentItem to null on CarouselView #13536

Closed
wants to merge 3 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
Expand Up @@ -12,6 +12,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
Expand All @@ -29,9 +30,11 @@
<Button Command="{Binding RemoveCommand}" AutomationId="btnRemove" Text="{Binding Path=Selected.Index, StringFormat='Remove {0}'}" BackgroundColor="LightGray" TextColor="Black" />
<Button Command="{Binding NextCommand}" AutomationId="btnNext" Text="&gt;" FontAttributes="Bold" BackgroundColor="LightGray" TextColor="Black" />
</StackLayout>
<Button Grid.Row="5" Text="Clear" Command="{Binding ClearCommand}" AutomationId="btnClear"/>
<Button Grid.Row="5" Grid.Column="1" Text="Set" Command="{Binding SetCommand}" AutomationId="btnSet"/>
<CarouselView
x:Name="carousel"
Grid.Row="5" Grid.ColumnSpan="2"
Grid.Row="6" Grid.ColumnSpan="2"
Loop="{Binding IsLoop}"
AutomationId="TheCarouselView"
ItemsSource="{Binding Items}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,7 @@ public CarouselViewModel(CarouselXamlSampleType type, bool loop, int initialItem
IsLoop = loop;
_type = type;

var items = new List<CarouselItem>();
for (int i = 0; i < initialItems; i++)
{
switch (_type)
{
case CarouselXamlSampleType.Peek:
items.Add(new CarouselItem(i, "cardBackground.png"));
break;
default:
items.Add(new CarouselItem(i));
break;
}
}

MessagingCenter.Subscribe<ExampleTemplateCarousel>(this, "remove", (obj) => Items.Remove(obj.BindingContext as CarouselItem));

Items = new ObservableCollection<CarouselItem>(items);
Count = Items.Count - 1;

if (startCurrentItem != -1)
Selected = Items[startCurrentItem];
SetItems(initialItems, startCurrentItem);
}

public bool IsLoop
Expand Down Expand Up @@ -124,6 +104,44 @@ public CarouselItem Selected
Selected = newItem;
}
});

public ICommand ClearCommand => new Command(ClearItems);

public ICommand SetCommand => new Command(() =>
{
SetItems(10, 5);
});

void ClearItems()
{
Items.Clear();
Selected = null;
}

void SetItems(int initialItems, int startCurrentItem)
{
var items = new List<CarouselItem>();
for (int i = 0; i < initialItems; i++)
{
switch (_type)
{
case CarouselXamlSampleType.Peek:
items.Add(new CarouselItem(i, "cardBackground.png"));
break;
default:
items.Add(new CarouselItem(i));
break;
}
}

MessagingCenter.Subscribe<ExampleTemplateCarousel>(this, "remove", (obj) => Items.Remove(obj.BindingContext as CarouselItem));

Items = new ObservableCollection<CarouselItem>(items);
Count = Items.Count - 1;

if (startCurrentItem != -1)
Selected = Items[startCurrentItem];
}
}

[Preserve(AllMembers = true)]
Expand All @@ -142,5 +160,10 @@ public CarouselItem(int index, string image = null)
public string Image { get; set; }

public string FeaturedImage { get; set; }

public override string ToString()
{
return Index.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,12 @@ void SetCurrentItem(int carouselPosition)

void UpdateFromCurrentItem()
{
var currentItemPosition = ItemsViewAdapter.ItemsSource.GetPosition(Carousel.CurrentItem);
var currentItem = Carousel?.CurrentItem;

if (currentItem == null)
return;

var currentItemPosition = ItemsViewAdapter.ItemsSource.GetPosition(currentItem);
var carouselPosition = Carousel.Position;

if (_gotoPosition == -1 && currentItemPosition != carouselPosition)
Expand Down