From a42c7a51980e4be8f310ddd4bda1362112a4f480 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 17 Aug 2016 23:57:14 -0400 Subject: [PATCH] Started extending android tableview more with CustomTableViewModelRenderer --- .../Controls/ExtendedTableViewRenderer.cs | 72 ++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/src/Android/Controls/ExtendedTableViewRenderer.cs b/src/Android/Controls/ExtendedTableViewRenderer.cs index 123b19fc1..ef086ebf4 100644 --- a/src/Android/Controls/ExtendedTableViewRenderer.cs +++ b/src/Android/Controls/ExtendedTableViewRenderer.cs @@ -10,20 +10,30 @@ using Bit.App.Controls; using Bit.App.Enums; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; +using Android.Content; +using AView = Android.Views.View; +using AListView = Android.Widget.ListView; +using Android.Views; +using Android.Util; [assembly: ExportRenderer(typeof(ExtendedTableView), typeof(ExtendedTableViewRenderer))] namespace Bit.Android.Controls { public class ExtendedTableViewRenderer : TableViewRenderer { + protected override TableViewModelRenderer GetModelRenderer(AListView listView, TableView view) + { + return new CustomTableViewModelRenderer(Context, listView, view); + } + public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint) { var baseSize = base.GetDesiredSize(widthConstraint, heightConstraint); var height = ComputeHeight(Control, Convert.ToInt32(baseSize.Request.Width)); - return new SizeRequest(new Size(baseSize.Request.Width, height)); + return new SizeRequest(new Xamarin.Forms.Size(baseSize.Request.Width, height)); } - private int ComputeHeight(global::Android.Widget.ListView listView, int width) + private int ComputeHeight(AListView listView, int width) { var adapter = listView.Adapter; var totalHeight = listView.PaddingTop + listView.PaddingBottom; @@ -38,5 +48,63 @@ namespace Bit.Android.Controls return totalHeight + (listView.DividerHeight * (adapter.Count - 1)); } + + private class CustomTableViewModelRenderer : TableViewModelRenderer + { + private readonly TableView _view; + private readonly AListView _listView; + + public CustomTableViewModelRenderer(Context context, AListView listView, TableView view) + : base(context, listView, view) + { + _view = view; + _listView = listView; + } + + private ITableViewController Controller => _view; + + public override AView GetView(int position, AView convertView, ViewGroup parent) + { + var baseView = base.GetView(position, convertView, parent); + bool isHeader, nextIsHeader; + GetCellPosition(position, out isHeader, out nextIsHeader); + if(isHeader) + { + baseView.SetBackgroundColor(Xamarin.Forms.Color.Transparent.ToAndroid()); + } + else + { + baseView.SetBackgroundColor(Xamarin.Forms.Color.Red.ToAndroid()); + } + + return baseView; + } + + private void GetCellPosition(int position, out bool isHeader, out bool nextIsHeader) + { + isHeader = false; + nextIsHeader = false; + + var model = Controller.Model; + var sectionCount = model.GetSectionCount(); + + for(var sectionIndex = 0; sectionIndex < sectionCount; sectionIndex++) + { + var size = model.GetRowCount(sectionIndex) + 1; + if(position == 0) + { + isHeader = true; + nextIsHeader = size == 0 && sectionIndex < sectionCount - 1; + } + + if(position < size) + { + nextIsHeader = position == size - 1; + } + + position -= size; + } + } + } } }