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

Commit

Permalink
[Android] ItemContentView should use all available space (#4569) fixes
Browse files Browse the repository at this point in the history
…#4406

* [Android] ItemContentView should use all available space; fixes #4406
Fix inconsistent/inaccurate class naming for cells/views.

* Update Xamarin.Forms.Platform.Android/CollectionView/SizedItemContentView.cs

Add missing space.

Co-Authored-By: hartez <hartez@users.noreply.github.com>

* Make ReuseId values match class names
  • Loading branch information
hartez authored and rmarinho committed Dec 11, 2018
1 parent 6f6511a commit 154c2d0
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 114 deletions.
Expand Up @@ -23,8 +23,8 @@ protected override void UpdateItemsSource()
// But for the Carousel, we want it to create the items to fit the width/height of the viewport
// So we give it an alternate delegate for creating the views

ItemsViewAdapter = new ItemsViewAdapter(ItemsView, Context,
(renderer, context) => new SizedItemContentControl(renderer, context, () => Width, () => Height));
ItemsViewAdapter = new ItemsViewAdapter(ItemsView,
(renderer, context) => new SizedItemContentView(renderer, context, () => Width, () => Height));

SwapAdapter(ItemsViewAdapter, false);
}
Expand Down
Expand Up @@ -47,14 +47,15 @@ public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int
}

// EmptyView is a Forms View; display that
var itemContentControl = new SizedItemContentControl(CreateRenderer(formsView, context), context,
var itemContentControl = new SizedItemContentView(CreateRenderer(formsView, context), context,
() => parent.Width, () => parent.Height);
return new EmptyViewHolder(itemContentControl, formsView);
}

// We have a template, so create a view from it
var templateElement = EmptyViewTemplate.CreateContent() as View;
var templatedItemContentControl = new SizedItemContentControl(CreateRenderer(templateElement, context), context, () => parent.Width, () => parent.Height);
var templatedItemContentControl = new SizedItemContentView(CreateRenderer(templateElement, context),
context, () => parent.Width, () => parent.Height);
return new EmptyViewHolder(templatedItemContentControl, templateElement);
}

Expand Down

This file was deleted.

57 changes: 57 additions & 0 deletions Xamarin.Forms.Platform.Android/CollectionView/ItemContentView.cs
@@ -0,0 +1,57 @@
using Android.Content;
using Android.Views;

namespace Xamarin.Forms.Platform.Android
{
internal class ItemContentView : ViewGroup
{
protected readonly IVisualElementRenderer Content;

public ItemContentView(IVisualElementRenderer content, Context context) : base(context)
{
Content = content;
AddContent();
}

void AddContent()
{
AddView(Content.View);
}

protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
var size = Context.FromPixels(r - l, b - t);

Content.Element.Layout(new Rectangle(Point.Zero, size));

Content.UpdateLayout();
}

protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int pixelWidth = MeasureSpec.GetSize(widthMeasureSpec);
int pixelHeight = MeasureSpec.GetSize(heightMeasureSpec);

var width = Context.FromPixels(pixelWidth);
var height = Context.FromPixels(pixelHeight);

SizeRequest measure = Content.Element.Measure(width, height, MeasureFlags.IncludeMargins);

if (pixelWidth == 0)
{
pixelWidth = (int)Context.ToPixels(Content.Element.Width > 0
? Content.Element.Width
: measure.Request.Width);
}

if (pixelHeight == 0)
{
pixelHeight = (int)Context.ToPixels(Content.Element.Height > 0
? Content.Element.Height
: measure.Request.Height);
}

SetMeasuredDimension(pixelWidth, pixelHeight);
}
}
}
Expand Up @@ -16,23 +16,20 @@ namespace Xamarin.Forms.Platform.Android
public class ItemsViewAdapter : RecyclerView.Adapter
{
protected readonly ItemsView ItemsView;
readonly Context _context;
readonly Func<IVisualElementRenderer, Context, AView> _createView;
readonly IItemsViewSource _itemsSource;

internal ItemsViewAdapter(ItemsView itemsView, Context context,
Func<IVisualElementRenderer, Context, AView> createView = null)
internal ItemsViewAdapter(ItemsView itemsView, Func<IVisualElementRenderer, Context, AView> createView = null)
{
CollectionView.VerifyCollectionViewFlagEnabled(nameof(ItemsViewAdapter));

ItemsView = itemsView;
_context = context;
_createView = createView;
_itemsSource = ItemsSourceFactory.Create(itemsView.ItemsSource, this);

if (_createView == null)
{
_createView = (renderer, context1) => new ItemContentControl(renderer, context1);
_createView = (renderer, context) => new ItemContentView(renderer, context);
}
}

Expand Down
Expand Up @@ -221,7 +221,7 @@ protected virtual void UpdateItemsSource()
// Stop watching the old adapter to see if it's empty (if we _are_ watching)
Unwatch(GetAdapter());

ItemsViewAdapter = new ItemsViewAdapter(ItemsView, Context);
ItemsViewAdapter = new ItemsViewAdapter(ItemsView);
SwapAdapter(ItemsViewAdapter, false);

UpdateEmptyView();
Expand Down

This file was deleted.

@@ -0,0 +1,29 @@
using System;
using Android.Content;

namespace Xamarin.Forms.Platform.Android
{
internal class SizedItemContentView : ItemContentView
{
readonly Func<int> _width;
readonly Func<int> _height;

public SizedItemContentView(IVisualElementRenderer content, Context context, Func<int> width, Func<int> height)
: base(content, context)
{
_width = width;
_height = height;
}

protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
var targetWidth = _width();
var targetHeight = _height();

Content.Element.Measure(Context.FromPixels(targetWidth), Context.FromPixels(targetHeight),
MeasureFlags.IncludeMargins);

SetMeasuredDimension(targetWidth, targetHeight);
}
}
}
Expand Up @@ -82,15 +82,15 @@
<Compile Include="CollectionView\EdgeSnapHelper.cs" />
<Compile Include="CollectionView\EndSnapHelper.cs" />
<Compile Include="CollectionView\IItemsViewSource.cs" />
<Compile Include="CollectionView\ItemContentControl.cs" />
<Compile Include="CollectionView\ItemContentView.cs" />
<Compile Include="CollectionView\ItemsSourceFactory.cs" />
<Compile Include="CollectionView\ItemsViewRenderer.cs" />
<Compile Include="CollectionView\ListSource.cs" />
<Compile Include="CollectionView\ObservableItemsSource.cs" />
<Compile Include="CollectionView\PositionalSmoothScroller.cs" />
<Compile Include="CollectionView\PropertyChangedEventArgsExtensions.cs" />
<Compile Include="CollectionView\ScrollHelper.cs" />
<Compile Include="CollectionView\SizedItemContentControl.cs" />
<Compile Include="CollectionView\SizedItemContentView.cs" />
<Compile Include="CollectionView\SnapManager.cs" />
<Compile Include="CollectionView\StartPagerSnapHelper.cs" />
<Compile Include="CollectionView\StartSnapHelper.cs" />
Expand Down
Expand Up @@ -139,13 +139,13 @@ string DetermineCellReusedId()
if (_itemsView.ItemTemplate != null)
{
return _layout.ScrollDirection == UICollectionViewScrollDirection.Horizontal
? TemplatedHorizontalListCell.ReuseId
: TemplatedVerticalListCell.ReuseId;
? HorizontalTemplatedCell.ReuseId
: VerticalTemplatedCell.ReuseId;
}

return _layout.ScrollDirection == UICollectionViewScrollDirection.Horizontal
? DefaultHorizontalListCell.ReuseId
: DefaultVerticalListCell.ReuseId;
? HorizontalDefaultCell.ReuseId
: VerticalDefaultCell.ReuseId;
}

UICollectionViewCell GetPrototype()
Expand All @@ -162,11 +162,11 @@ UICollectionViewCell GetPrototype()

void RegisterCells()
{
CollectionView.RegisterClassForCell(typeof(DefaultHorizontalListCell), DefaultHorizontalListCell.ReuseId);
CollectionView.RegisterClassForCell(typeof(DefaultVerticalListCell), DefaultVerticalListCell.ReuseId);
CollectionView.RegisterClassForCell(typeof(TemplatedHorizontalListCell),
TemplatedHorizontalListCell.ReuseId);
CollectionView.RegisterClassForCell(typeof(TemplatedVerticalListCell), TemplatedVerticalListCell.ReuseId);
CollectionView.RegisterClassForCell(typeof(HorizontalDefaultCell), HorizontalDefaultCell.ReuseId);
CollectionView.RegisterClassForCell(typeof(VerticalDefaultCell), VerticalDefaultCell.ReuseId);
CollectionView.RegisterClassForCell(typeof(HorizontalTemplatedCell),
HorizontalTemplatedCell.ReuseId);
CollectionView.RegisterClassForCell(typeof(VerticalTemplatedCell), VerticalTemplatedCell.ReuseId);
}
}
}
Expand Up @@ -3,13 +3,12 @@

namespace Xamarin.Forms.Platform.iOS
{
// TODO hartez 2018/09/12 21:43:54 The "List" part of all these cell names needs to go
internal sealed class DefaultHorizontalListCell : DefaultCell
internal sealed class HorizontalDefaultCell : DefaultCell
{
public static NSString ReuseId = new NSString("Xamarin.Forms.Platform.iOS.DefaultHorizontalListCell");
public static NSString ReuseId = new NSString("Xamarin.Forms.Platform.iOS.HorizontalDefaultCell");

[Export("initWithFrame:")]
public DefaultHorizontalListCell(CGRect frame) : base(frame)
public HorizontalDefaultCell(CGRect frame) : base(frame)
{
Constraint = Label.HeightAnchor.ConstraintEqualTo(Frame.Height);
Constraint.Active = true;
Expand Down
Expand Up @@ -5,12 +5,12 @@

namespace Xamarin.Forms.Platform.iOS
{
internal sealed class TemplatedHorizontalListCell : TemplatedCell
internal sealed class HorizontalTemplatedCell : TemplatedCell
{
public static NSString ReuseId = new NSString("Xamarin.Forms.Platform.iOS.TemplatedHorizontalListCell");
public static NSString ReuseId = new NSString("Xamarin.Forms.Platform.iOS.HorizontalTemplatedCell");

[Export("initWithFrame:")]
public TemplatedHorizontalListCell(CGRect frame) : base(frame)
public HorizontalTemplatedCell(CGRect frame) : base(frame)
{
}

Expand Down
Expand Up @@ -3,12 +3,12 @@

namespace Xamarin.Forms.Platform.iOS
{
internal sealed class DefaultVerticalListCell : DefaultCell
internal sealed class VerticalDefaultCell : DefaultCell
{
public static NSString ReuseId = new NSString("Xamarin.Forms.Platform.iOS.DefaultVerticalListCell");
public static NSString ReuseId = new NSString("Xamarin.Forms.Platform.iOS.VerticalDefaultCell");

[Export("initWithFrame:")]
public DefaultVerticalListCell(CGRect frame) : base(frame)
public VerticalDefaultCell(CGRect frame) : base(frame)
{
Constraint = Label.WidthAnchor.ConstraintEqualTo(Frame.Width);
Constraint.Active = true;
Expand Down
Expand Up @@ -6,12 +6,12 @@

namespace Xamarin.Forms.Platform.iOS
{
internal sealed class TemplatedVerticalListCell : TemplatedCell
internal sealed class VerticalTemplatedCell : TemplatedCell
{
public static NSString ReuseId = new NSString("Xamarin.Forms.Platform.iOS.TemplatedVerticalListCell");
public static NSString ReuseId = new NSString("Xamarin.Forms.Platform.iOS.VerticalTemplatedCell");

[Export("initWithFrame:")]
public TemplatedVerticalListCell(CGRect frame) : base(frame)
public VerticalTemplatedCell(CGRect frame) : base(frame)
{
}

Expand Down
8 changes: 4 additions & 4 deletions Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.csproj
Expand Up @@ -123,17 +123,17 @@
<Compile Include="CollectionView\ItemsViewCell.cs" />
<Compile Include="CollectionView\CollectionViewController.cs" />
<Compile Include="CollectionView\DefaultCell.cs" />
<Compile Include="CollectionView\DefaultHorizontalListCell.cs" />
<Compile Include="CollectionView\DefaultVerticalListCell.cs" />
<Compile Include="CollectionView\HorizontalDefaultCell.cs" />
<Compile Include="CollectionView\VerticalDefaultCell.cs" />
<Compile Include="CollectionView\GridViewLayout.cs" />
<Compile Include="CollectionView\ItemsViewLayout.cs" />
<Compile Include="CollectionView\ListSource.cs" />
<Compile Include="CollectionView\ListViewLayout.cs" />
<Compile Include="CollectionView\ObservableItemsSource.cs" />
<Compile Include="CollectionView\PropertyChangedEventArgsExtensions.cs" />
<Compile Include="CollectionView\TemplatedCell.cs" />
<Compile Include="CollectionView\TemplatedHorizontalListCell.cs" />
<Compile Include="CollectionView\TemplatedVerticalListCell.cs" />
<Compile Include="CollectionView\HorizontalTemplatedCell.cs" />
<Compile Include="CollectionView\VerticalTemplatedCell.cs" />
<Compile Include="DisposeHelpers.cs" />
<Compile Include="EffectUtilities.cs" />
<Compile Include="ExportCellAttribute.cs" />
Expand Down

0 comments on commit 154c2d0

Please sign in to comment.