billvenhaus / ReorderableCollectionView.Maui

ReorderableCollectionView for .NET MAUI
19 stars 7 forks source link

Automatic margin #2

Open lauglam opened 2 years ago

lauglam commented 2 years ago

Hi, I'm using the library you wrote and it's really awesome, thanks a lot.

I'm a newbie to using layouts and I want to be centered when a line is full and at the start when it's not full. Is there any way to do this please?

image

image

Or is it possible to make the margin property automatically generated? Sorry, I'm a newbie, if you have any suggestions, please let me know, thank you.

lauglam commented 2 years ago

I wrote a very simple code that does what I want, but it looks weird.

internal class MangaReorderableCollectionView : ReorderableCollectionView.Maui.ReorderableCollectionView
{
    protected override Size MeasureOverride(double widthConstraint, double heightConstraint)
    {
        var length = 0;
        foreach (var item in ItemsSource)
        {
            length++;
        }

        if (length > 0)
        {
            if (ItemsLayout is VariableSpanGridItemsLayout gridItemsLayout)
            {
                var lineCount = (int)(widthConstraint / gridItemsLayout.ItemWidth);
                if (length < lineCount) HorizontalOptions = LayoutOptions.Start;
                else HorizontalOptions = LayoutOptions.Center;

                var blank = widthConstraint - lineCount * gridItemsLayout.ItemWidth;
                var spacing = blank / (lineCount + 1);
                Margin = new Thickness(spacing, Margin.Top, spacing, Margin.Bottom);
                gridItemsLayout.HorizontalItemSpacing = spacing;
            }
        }
        return base.MeasureOverride(widthConstraint, heightConstraint);
    }
}
lauglam commented 2 years ago

image