I want to create control in Xamarin.Forms that uses native android RecyclerView as renderer.
My problem occurs when i try create cell that contains labels or other controls with HorizontalOptions different than: Default, Fill and FillAndExpand. That labels just don't render themselves (Screenshot). thisis my .xaml cell code
<?xml version="1.0" encoding="utf-8" ?>
<self:FormsRecyclerCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:self="clr-namespace:FormsRecyclerViewApp;assembly:FormsRecyclerViewApp" x:Class="FormsRecyclerViewApp.FormsRecyclerCellTemplate">
<Grid>
<Label Grid.Row="0" Text="{Binding .}" BackgroundColor="Blue" />
<Label Grid.Row="1" HorizontalOptions="Start" Text="Horizontal Start" BackgroundColor="AliceBlue"/>
<Label Grid.Row="2" HorizontalOptions="StartAndExpand" Text="Horizontal StartAndExpand" BackgroundColor="AliceBlue" />
<Label Grid.Row="3" HorizontalOptions="Fill" Text="Horizontal Fill" BackgroundColor="Aqua" />
<Label Grid.Row="4" HorizontalOptions="FillAndExpand" Text="Horizontal FillAndExpand" BackgroundColor="Aqua" />
<Label Grid.Row="5" HorizontalOptions="Center" Text="Horizontal Center" BackgroundColor="Bisque" />
<Label Grid.Row="6" HorizontalOptions="CenterAndExpand" Text="Horizontal CenterAndExpand" BackgroundColor="Bisque" />
<Label Grid.Row="7" HorizontalOptions="End" Text="Horizontal End" BackgroundColor="BlanchedAlmond" />
<Label Grid.Row="8" HorizontalOptions="EndAndExpand" Text="Horizontal EndAndExpand" BackgroundColor="BlanchedAlmond" />
<Label Grid.Row="9" Text="Horizontal Default" BackgroundColor="Azure"/>
<BoxView Grid.Row="10" BackgroundColor="Green" />
</Grid>
</self:FormsRecyclerCell>
For rendering things inside RecyclerView i'm using CellContainer that creates PlatformRenderer and overrides methods OnLayout and OnMeasure
FormsRecyclerViewCellContainer.cs
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
using (var handler = new Handler(Looper.MainLooper))
{
handler.Post(() =>
{
double width = Context.FromPixels(r - l);
double height = Context.FromPixels(b - t);
Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(this.view.Element, new XForms.Rectangle(0, 0, width, height));
this.view.UpdateLayout();
});
}
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (this.view == null || this.view.Element == null)
{
base.SetMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
return;
}
XForms.SizeRequest measure = this.view.Element.Measure(this.initialWidth, double.PositiveInfinity, XForms.MeasureFlags.IncludeMargins);
int height = (int)Context.ToPixels(this.fastCell.Height > 0 ? this.fastCell.Height : measure.Request.Height);
this.SetMeasuredDimension((int)Context.ToPixels(this.initialWidth), height);
}
My questions:
- What is the best way to get native objects from forms .xaml?
- Why some labels don't render themselves?
- What should i do to render all VisualElemts properly?
Feel free to download my sample project.