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

[Bug] [Android] Collection View wrong first/last item sizing (on Orientation=Vertical and Span > 1) #9125

Closed
inimirpaz opened this issue Jan 8, 2020 · 11 comments · Fixed by #10624

Comments

@inimirpaz
Copy link

inimirpaz commented Jan 8, 2020

Description

When using a Collection View on Android (any device) with Orientation=Vertical and Span > 1 (to get more than one column), when the DataTemplate is composed of a grid of fixed height and width, if the last item happens to sit on the rightmost slot its height is messed up. Also, the width of the first item is always messed up on ItemSizingStrategy=MeasureFirstItem . (see attached project)

Steps to Reproduce

  1. Have a DataTemplate of a Collection View composed of a Grid view with fixed height (rowdefinitions) and width (columndefinitions)
  2. Populate the ItemSource so that the last item lands on the rightmost column of the collection view.

Expected Behavior

First/last item on a Collection View to always be the size specified by the Grid col/row definitions.

Actual Behavior

If, and only if, the last item happens to sit on the rightmost column in a Collection view it gets its height messed up. If it happens to be in any other column the item gets properly sized.
When the sizing strategy is set to MeasureFirstItem, the first item always gets its width messed up.

Basic Information

  • Version with issue: 4.4.0.991265 (known since at least 4.2, and still present in 4.6.0.129-nightly)
  • Last known good version: N/A
  • IDE: VS for Mac 8.3.11 (build 1)
  • Android: devices with API 23 -> 28 for sure.

Screenshots

Last item (wrong height):
Screen1

When ItemSizingStrategy="MeasureFirstItem", the first item gets wrong width as well:
Screen2

Workaround

Add an "invisible" (enabled=false, visible=false) grid element to the Collection view in order to get the last (visible) item sized properly not being the last item in the collection anymore.

Reproduction Link

CollectionViewBug.zip

@inimirpaz inimirpaz added s/unverified New report that has yet to be verified t/bug 🐛 labels Jan 8, 2020
@pauldipietro pauldipietro added this to New in Triage Jan 8, 2020
@hartez hartez added p/Android and removed s/unverified New report that has yet to be verified labels Jan 9, 2020
@hartez hartez added this to Backlog in CollectionView via automation Jan 9, 2020
@hartez hartez removed this from New in Triage Jan 9, 2020
@hartez hartez added the e/4 🕓 4 label Jan 9, 2020
@keozx
Copy link

keozx commented Jan 22, 2020

A span of 3 or more makes the first row/column's items also bigger than the rest, probably different bug, will try to issue one

@keozx
Copy link

keozx commented Jan 22, 2020

There seems to be a nasty issue with HorizontalItemSpacing and VerticalItemSpacing, in my case I could fix my layout wrapping a Grid around the actual item in the template, which should not be needed, but if I set any item spacing it's all messy sizing, this is with a Span of 9 and 45 items, so you are only left with Padding on the Grid as a workaround

@ttmher
Copy link

ttmher commented Jan 23, 2020

I have the same problem. Adding a footer with an element of 0 height fixed the problem for me.

@inimirpaz
Copy link
Author

inimirpaz commented Jan 24, 2020

I have the same problem. Adding a footer with an element of 0 height fixed the problem for me.

Nice! Same here, it's a better workaround than mine for sure.
(Not that I was expecting otherwise but ItemSizingStrategy="MeasureFirstItem" still makes the first item wider)

@aranor01
Copy link

The workaround of adding an "invisible" footer can't be used if you actually need a footer, in fact in this case the footer width will be altered.

@IliyanPopov
Copy link

Issue sill not resolved. Tested with Xamarin.Forms v4.6.0.529-pre3 (latest pre-release at the moment)

@ChummerUA
Copy link
Contributor

ChummerUA commented May 6, 2020

Source of this issue is SpacingItemDecoration
https://github.com/xamarin/Xamarin.Forms/blob/7a52542d50797ccc69ae1d8dd84259190d96bdb4/Xamarin.Forms.Platform.Android/CollectionView/SpacingItemDecoration.cs
The workaround is to create custom ItemDecoration with correct outRect in GetItemsOffset and override CreateSpacingDecoration in CollectionView renderer.

@ChummerUA
Copy link
Contributor

ChummerUA commented May 8, 2020

Found out that it's not enough to just fix ItemDecoration's outRect. Decorations are not updated correctly when collection is updated. To fix this you need to notify adapter that collection is updated.

@chaoyebugao
Copy link

met the same problem.

@chaoyebugao
Copy link

chaoyebugao commented May 21, 2020

I solved this by add external StackLayout as parent in DataTemplate then set it's child's background.
Just like below:

<DataTemplate>
	<StackLayout> 
		<Frame CornerRadius="8" IsClippedToBounds="True" Padding="0" >
			<Grid RowSpacing="4" Padding="12,10,10,10" BackgroundColor="LightGray">
				<Grid.RowDefinitions>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto" />
					<RowDefinition Height="Auto" />
				</Grid.RowDefinitions>

				<RelativeLayout Grid.Row="0">
					<Label Text="0"/>
				</RelativeLayout>

				<RelativeLayout Grid.Row="1">
					<Label Text="1"/>
				</RelativeLayout>

				<RelativeLayout Grid.Row="2">
					<Label Text="2"/>
				</RelativeLayout>

			</Grid>
		</Frame>
	</StackLayout>
</DataTemplate>

@samhouts samhouts added the in-progress This issue has an associated pull request that may resolve it! label May 28, 2020
@samhouts samhouts moved this from Backlog to In Progress in CollectionView May 28, 2020
@samhouts samhouts added this to In Progress in 4.7.0 Jun 20, 2020
@samhouts samhouts removed this from In Progress in 4.7.0 Jul 8, 2020
@samhouts samhouts added this to In Progress in vCurrent (4.8.0) Jul 30, 2020
@samhouts samhouts added this to In Progress in .NET MAUI Backlog Jul 30, 2020
@samhouts samhouts removed this from In Progress in vCurrent (4.8.0) Jul 30, 2020
@samhouts samhouts removed this from In Progress in .NET MAUI Backlog Aug 4, 2020
@samhouts samhouts added this to In Progress in vNext+1 (5.0.0) Aug 11, 2020
@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
@ahmedroshdy
Copy link

same issue happens with

CollectionView automation moved this from In Progress to Done Dec 18, 2020
vNext+1 (5.0.0) automation moved this from In Progress to Done Dec 18, 2020
rmarinho added a commit that referenced this issue Dec 18, 2020
…#10624) fixes #9125 fixes #10585 fixes #13054 fixes #9681

* -fixed incorrect outRect values

* fixed unassigned offsets

* -fixed incorrect lastSpanIndex and outRect values

* -fixed itemdecorations stayed same when collection is updated

* Fix indexes in notifications

* Fix hang when inserting item in CarouselView

* Fix timing issue with Carousel current item when inserting

* Use GetSpanGroupIndex to determine first row/column

* Fix starting index for looped CarouselView

* Fix Carousel tests so they don't all fail when one fails;
Fix Carousel tests so they don't take 30 minutes to run;

* Fix index offset on looped Carousel;

Co-authored-by: E.Z. Hart <hartez@gmail.com>
Co-authored-by: Rui Marinho <me@ruimarinho.net>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Development

Successfully merging a pull request may close this issue.