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

[Enhancement] [CollectionView] Allow ItemTemplate to set SpanSize in grid layout mode #6357

Closed
PawKanarek opened this issue May 30, 2019 · 2 comments
Labels
a/collectionview inactive Issue is older than 6 months and needs to be retested proposal-open t/enhancement ➕

Comments

@PawKanarek
Copy link
Contributor

Summary

CollectionView right now (X.F 4.0.0.425677) doesn't have option to set ColumnSpan/RowSpan for ItemTemplates in Vertical/Horizontal grid layout mode.

API Changes

In Android GridLayoutManager there is option to set SpanSizeLookup, so implementation would be fairly easy.

// Idea: 
public class SpanSizeLookup : GridLayoutManager.SpanSizeLookup
{
    private readonly Adapter adapter;
    private readonly GridLayoutManager layoutManager;

    public SpanSizeLookup(Adapter adapter, GridLayoutManager layoutManager)
    {
        this.adapter = adapter;
        this.layoutManager = layoutManager;
    }

    public override int GetSpanSize(int position)
    {
        var item = this.adapter.Items.Cast<object>().ElementAt(position);

        if (item is ISpanDefintion spanDefinition)
        {
            return spanDefinition.SpanCount;
        }
        else
        {
            return 1;
        }
    }
}

iOS however can more hacky, because as far i know, there is no mechanism for defining span size for items, but UIColectionView can be very handy with different view cell sizes. So maybe manipulating cell size inside layoutAttributesForItemAtIndexPath method inside UICollectionViewFlowLayout would accomplish this feature: (code from stackoverflow answer)

  1. Create the following flow layout subclass
class HorizontallyFlushCollectionViewFlowLayout: UICollectionViewFlowLayout {

    // Don't forget to use this class in your storyboard (or code, .xib etc)

    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)?.copy() as? UICollectionViewLayoutAttributes
        guard let collectionView = collectionView else { return attributes }
        attributes?.bounds.size.width = collectionView.bounds.width - sectionInset.left - sectionInset.right
        return attributes
    }

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let allAttributes = super.layoutAttributesForElementsInRect(rect)
        return allAttributes?.flatMap { attributes in
            switch attributes.representedElementCategory {
            case .Cell: return layoutAttributesForItemAtIndexPath(attributes.indexPath)
            default: return attributes
            }
        }
    }
}
  1. Register your collection view for automatic sizing
// The provided size should be a plausible estimate of the actual
// size. You can set your item size in your storyboard
// to a good estimate and use the code below. Otherwise,
// you can provide it manually too, e.g. CGSize(width: 100, height: 100)

flowLayout.estimatedItemSize = flowLayout.itemSize
  1. Use the predefined width + custom height in your cell subclass
override func preferredLayoutAttributesFittingAttributes(layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
    layoutAttributes.bounds.size.height = systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
    return layoutAttributes
}

The iOS part is obviously harder, but i think that everyone would love to use this feature in their apps.

Intended Use Case

We could use this enhancement to create layouts that looks like this:
image

If idea with making customs span sizes is too hard, then for first iteration of this enhancement you could only add the possibility to render items with FullSpanSize (there is property in Android StaggeredGridLayoutManager to achieve this) so we could make layout like this:
image

@pauldipietro pauldipietro added this to New in Triage May 30, 2019
@PawKanarek PawKanarek changed the title [Enhancement] CollectionView allow ItemTemplate to set SpanSize in Grid layout mode [Enhancement] [CollectionView] Allow ItemTemplate to set SpanSize in grid layout mode May 30, 2019
@samhouts samhouts removed this from New in Triage May 30, 2019
@samhouts samhouts added this to Under consideration in Enhancements Jun 4, 2019
@samhouts samhouts added this to Backlog in CollectionView Jul 25, 2019
@samhouts samhouts added inactive Issue is older than 6 months and needs to be retested help wanted We welcome community contributions to any issue, but these might be a good place to start! up-for-grabs We welcome community contributions to any issue, but these might be a good place to start! labels Nov 26, 2019
@samhouts samhouts removed help wanted We welcome community contributions to any issue, but these might be a good place to start! up-for-grabs We welcome community contributions to any issue, but these might be a good place to start! labels Feb 4, 2020
@samhouts samhouts moved this from Under consideration to Under consideration-inactive in Enhancements Feb 12, 2020
@samhouts samhouts moved this from Backlog to Inactive in CollectionView May 26, 2020
@GeorgeVelikov
Copy link

I was surprised this wasn't considered yet tbh, it feels like such a common UI paradigm

@jfversluis
Copy link
Member

Thanks for this suggestion! As Xamarin.Forms is now in maintenance mode, this will not happen anymore for Xamarin.Forms. We're only adding bugfixes and stability fixes.

If this is still important to you, make sure to check the .NET MAUI repo and see if it's already on the roadmap. If not, feel free to open a discussion to discuss a change first or open an issue with a detailed feature request. Thanks!

@jfversluis jfversluis closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2022
Enhancements automation moved this from Under consideration-inactive to Closed Aug 23, 2022
CollectionView automation moved this from Inactive to Done Aug 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/collectionview inactive Issue is older than 6 months and needs to be retested proposal-open t/enhancement ➕
Projects
Enhancements
  
Closed
Development

No branches or pull requests

4 participants