Skip to content

Commit

Permalink
Add/Remove CarouselView Layout Listener when view is add/removed from…
Browse files Browse the repository at this point in the history
… window (#18771)

* Don't fire layout if CV is disposed

* - add/remove listeners from window attached

* - make sure ItemsView is set before running Layout Ready code

* - keep setups

* - change clear/add order
  • Loading branch information
PureWeen committed Nov 16, 2023
1 parent 417acea commit 573dfb9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public override void TearDownOldElement(CarouselView oldElement)
base.TearDownOldElement(oldElement);
}

protected override void OnAttachedToWindow()
{
AddLayoutListener();
base.OnAttachedToWindow();
}

protected override void OnDetachedFromWindow()
{
ClearLayoutListener();
base.OnDetachedFromWindow();
}

public override void UpdateAdapter()
{
// By default the CollectionViewAdapter creates the items at whatever size the template calls for
Expand Down Expand Up @@ -513,7 +525,7 @@ void IMauiCarouselRecyclerView.UpdateFromPosition()

void AddLayoutListener()
{
if (_carouselViewLayoutListener != null)
if (_carouselViewLayoutListener is not null)
return;

_carouselViewLayoutListener = new CarouselViewOnGlobalLayoutListener(this);
Expand All @@ -522,6 +534,9 @@ void AddLayoutListener()

void LayoutReady()
{
if (ItemsView is null)
return;

if (!_initialized)
{
ItemsView.Scrolled += CarouselViewScrolled;
Expand Down Expand Up @@ -603,7 +618,8 @@ public CarouselViewOnGlobalLayoutListener(MauiCarouselRecyclerView recyclerView)

public void OnGlobalLayout()
{
if (_recyclerView.TryGetTarget(out var recyclerView))
if (_recyclerView.TryGetTarget(out var recyclerView) &&
recyclerView.IsAlive())
{
recyclerView.LayoutReady();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Microsoft.Maui.Controls.PointerGestureRecognizer.PointerReleasedCommand.get -> S
Microsoft.Maui.Controls.PointerGestureRecognizer.PointerReleasedCommand.set -> void
Microsoft.Maui.Controls.PointerGestureRecognizer.PointerReleasedCommandParameter.get -> object!
Microsoft.Maui.Controls.PointerGestureRecognizer.PointerReleasedCommandParameter.set -> void
override Microsoft.Maui.Controls.Handlers.Items.MauiCarouselRecyclerView.OnAttachedToWindow() -> void
override Microsoft.Maui.Controls.Handlers.Items.MauiCarouselRecyclerView.OnDetachedFromWindow() -> void
static readonly Microsoft.Maui.Controls.KeyboardAccelerator.KeyProperty -> Microsoft.Maui.Controls.BindableProperty!
static readonly Microsoft.Maui.Controls.KeyboardAccelerator.ModifiersProperty -> Microsoft.Maui.Controls.BindableProperty!
static readonly Microsoft.Maui.Controls.DragGestureRecognizer.CanDragProperty -> Microsoft.Maui.Controls.BindableProperty!
Expand Down Expand Up @@ -197,7 +199,6 @@ Microsoft.Maui.Controls.PlatformPointerEventArgs.Sender.get -> Android.Views.Vie
~static readonly Microsoft.Maui.Controls.ContentPage.HideSoftInputOnTappedProperty -> Microsoft.Maui.Controls.BindableProperty
Microsoft.Maui.Controls.ContentPage.HideSoftInputOnTapped.get -> bool
Microsoft.Maui.Controls.ContentPage.HideSoftInputOnTapped.set -> void

*REMOVED*~Microsoft.Maui.Controls.Editor.FontFamily.get -> string
*REMOVED*~Microsoft.Maui.Controls.Editor.FontFamily.set -> void
*REMOVED*Microsoft.Maui.Controls.Editor.CursorPosition.get -> int
Expand All @@ -210,7 +211,6 @@ Microsoft.Maui.Controls.ContentPage.HideSoftInputOnTapped.set -> void
*REMOVED*Microsoft.Maui.Controls.Editor.FontSize.set -> void
*REMOVED*Microsoft.Maui.Controls.Editor.SelectionLength.get -> int
*REMOVED*Microsoft.Maui.Controls.Editor.SelectionLength.set -> void

*REMOVED*~Microsoft.Maui.Controls.SearchBar.FontFamily.get -> string
*REMOVED*~Microsoft.Maui.Controls.SearchBar.FontFamily.set -> void
*REMOVED*Microsoft.Maui.Controls.SearchBar.CursorPosition.get -> int
Expand All @@ -223,7 +223,6 @@ Microsoft.Maui.Controls.ContentPage.HideSoftInputOnTapped.set -> void
*REMOVED*Microsoft.Maui.Controls.SearchBar.FontSize.set -> void
*REMOVED*Microsoft.Maui.Controls.SearchBar.SelectionLength.get -> int
*REMOVED*Microsoft.Maui.Controls.SearchBar.SelectionLength.set -> void

*REMOVED*~Microsoft.Maui.Controls.Entry.FontFamily.get -> string
*REMOVED*~Microsoft.Maui.Controls.Entry.FontFamily.set -> void
*REMOVED*Microsoft.Maui.Controls.Entry.CursorPosition.get -> int
Expand Down

0 comments on commit 573dfb9

Please sign in to comment.