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

[Bug] Wrong number of items per row when rotating CollectionView #7897

Closed
aliriomendes opened this issue Oct 9, 2019 · 8 comments
Closed

Comments

@aliriomendes
Copy link

Description

I'm trying to have a dynamic number of item per row depending on the screen width.
When I rotate the device and recalcula the number of items per row (GridItemsLayout.Span), the math is right but collection view doesn't behave accordantly on iOS.

private void CollectionView_SizeChanged(object sender, EventArgs e)
{
    if (sender is CollectionView collectionView && collectionView.ItemsLayout is GridItemsLayout gridItemsLayout)
    {
        gridItemsLayout.Span = Math.Max(1, (int)Math.Floor(collectionView.Width / 320));
        System.Diagnostics.Debug.WriteLine("Items per row -> " + gridItemsLayout.Span);
    }
}

Steps to Reproduce

  1. Rotate device

Expected Behavior

Actual Behavior

Basic Information

  • Version with issue:
    Any version of Xamarin.Forms

  • Platform Target Frameworks:

    • iOS:
  • Affected Devices:
    iPad - Number of items in screen is different from GridItemsLayout.Span
    iPhone - Number of items in screen is different from GridItemsLayout.Span when rotate to Horizontal and and item desapear when rotate to Vertical.

Screenshots

Simulator Screen Shot - iPad Air (3rd generation) - 2019-10-09 at 14 12 35
Simulator Screen Shot - iPad Air (3rd generation) - 2019-10-09 at 14 13 03

Reproduction Link

FormsCollectionViewRotationTest.zip

@aliriomendes aliriomendes added s/unverified New report that has yet to be verified t/bug 🐛 labels Oct 9, 2019
@pauldipietro pauldipietro added this to New in Triage Oct 9, 2019
@aliriomendes aliriomendes changed the title [Bug] [Bug] Wrong number of items per row when rotating CollectionView Oct 9, 2019
@aliriomendes
Copy link
Author

I found a quick workaround:

if (sender is CollectionView collectionView && collectionView.ItemsLayout is GridItemsLayout gridItemsLayout)
{
    if (Device.RuntimePlatform == Device.iOS)
        await Task.Yield();
    gridItemsLayout.Span = Math.Max(1, (int)Math.Floor(collectionView.Width / 320));
}

@jfversluis
Copy link
Member

jfversluis commented Oct 10, 2019

I see some strange things going on. When rotating back the number of items is different than before on iPad and on iPhone (11) whenever I rotate back the screen just goes blank. This latter case might be due to new code changes that aren't in the NuGet yet.

Enough to investigate this further!

@aliriomendes did you happen to test this also on Android?

@jfversluis jfversluis added a/collectionview and removed s/unverified New report that has yet to be verified labels Oct 10, 2019
@aliriomendes
Copy link
Author

I see some strange things going on. When rotating back the number of items is different than before on iPad and on iPhone (11) whenever I rotate back the screen just goes blank. This latter case might be due to new code changes that aren't in the NuGet yet.

Enough to investigate this further!

@aliriomendes did you happen to test this also on Android?

Yes, I tested it, and it works fine on Android.

@jfversluis jfversluis moved this from New to Ready For Work in Triage Oct 10, 2019
@samhouts samhouts moved this from Ready For Work to Needs Estimate in Triage Oct 10, 2019
@samhouts samhouts added this to Backlog in CollectionView Oct 11, 2019
@Jared-Aus
Copy link

Jared-Aus commented Jan 1, 2020

I'm finding the whole GridItemsLayout class to be very static. In my testing so far (iOS only), the HorizontalItemSpacing, VerticalItemSpacing, and Span properties don't work with DynamicResource bindings. I also couldn't get the Span property to bind to my ViewModel.

Update: I found I can dynamically bind and swap in a different ItemsLayout to work around different spans and spacing for portrait and landscape orientations, however I then hit the same bug reported here. As soon as I rotate from landscape to portrait the layout is messed up. Starting in portrait is ok, and rotating back to landscape is always ok.

@samhouts samhouts added the e/4 🕓 4 label Jan 3, 2020
@samhouts samhouts moved this from Needs Estimate to Ready For Work in Triage Jan 3, 2020
@samhouts samhouts added this to To do in iOS Ready For Work Jan 3, 2020
@samhouts samhouts removed this from Ready For Work in Triage Jan 3, 2020
@hs79hs
Copy link

hs79hs commented Feb 16, 2020

Does anybody have a solution for this? I also found this issue. When rotate back, if the span is set to 2 or 3, it works like the span is 1. And if I set the span to 4, it shows like the span is 2

@ryl
Copy link
Contributor

ryl commented Feb 26, 2020

I'm encountering this problem on an iPhoneXr with iOS 13.3.1

@borgdylan
Copy link

any news on this?

@hartez
Copy link
Contributor

hartez commented Jun 14, 2020

This is fixed in 4.6.0.847.

@hartez hartez closed this as completed Jun 14, 2020
iOS Ready For Work automation moved this from To do to Done Jun 14, 2020
CollectionView automation moved this from Backlog to Done Jun 14, 2020
@samhouts samhouts removed this from Done in iOS Ready For Work Jul 14, 2020
@samhouts samhouts removed this from Done in CollectionView Jul 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants