// --------------------------------------------------------------------- #endregion #region Private methods /* buildGrid =========================================================== * * Build the puzzle grid. This function creates the puzzle grid as a * 3x3 grid spaced evenly across the screen. Each grid cell contains * another 3x3 grid and each cell of the subgrids contains a textview * that can be accessed by IDs 0 - 80. * * ------------------------------------------------------------------ */ private void buildGrid() { //A var to assign unique IDs to each textview cell in the grid. int idCounter; //Loop counters. int xLoop; int yLoop; #region Top level grid layout: //Create a square percent layout to hold the main grid. SquarePercentLayout mainGridLayout = new SquarePercentLayout(myMainActivity); //Set main grid children to wrap. PercentRelativeLayout.LayoutParams mainGridLayoutParms = new PercentRelativeLayout.LayoutParams( ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent ); //Set main grid layout parms. mainGridLayout.LayoutParameters = mainGridLayoutParms; //Set main grid background. mainGridLayout.SetBackgroundColor(Android.Graphics.Color.Black); //Add main grid to top layout. this.AddView(mainGridLayout); #endregion //Init ID counter. idCounter = 0; //For each subgrid in the main grid... for (xLoop = 0; xLoop < 9; xLoop++) { #region Subgrid outer frame: (LinearLayout) //Create subgrid outer frame. LinearLayout subGridFrame = new LinearLayout(myMainActivity); //Create parameters for subgrid frame. PercentRelativeLayout.LayoutParams subGridFrameLayoutParms = new PercentRelativeLayout.LayoutParams( ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent ); //Get percent layout helper for subgrid frame. PercentLayoutHelper.PercentLayoutInfo pctLayoutHelper = subGridFrameLayoutParms.PercentLayoutInfo; //Set left and top margins. pctLayoutHelper.LeftMarginPercent = ((xLoop % 3) * (1 / 3f)); pctLayoutHelper.TopMarginPercent = ((xLoop / 3) * (1 / 3f)); //Set subgrid container width to 1/3 of parent. pctLayoutHelper.WidthPercent = (1 / 3f); //Set background and padding for large gridlines. subGridFrame.SetBackgroundColor(Android.Graphics.Color.Black); subGridFrame.SetPadding(2, 2, 2, 2); //Set outer frame parms. subGridFrame.LayoutParameters = subGridFrameLayoutParms; //Set to center child view. subGridFrame.SetGravity(GravityFlags.Center); //Add subgrid frame to main grid. mainGridLayout.AddView(subGridFrame); #endregion #region Subgrid layout: (SquarePercentLayout) //Create a new square percent layout for the subgrid. SquarePercentLayout subGridLayout = new SquarePercentLayout(myMainActivity); //Set subgrid layout to match parent. PercentRelativeLayout.LayoutParams subGridLayoutParms = new PercentRelativeLayout.LayoutParams( ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent ); //Set subgrid layout parms. subGridLayout.LayoutParameters = subGridLayoutParms; //Set subgrid background. subGridLayout.SetBackgroundColor(Android.Graphics.Color.Black); subGridFrame.RequestLayout(); //Add subgrid to subgrid frame. subGridFrame.AddView(subGridLayout); #endregion //For each cell in subgrid... for (yLoop = 0; yLoop < 9; yLoop++) { #region Number field outer frame: (LinearLayout) //Create a linear layout for the num holder outer frame. LinearLayout numFieldLayout = new LinearLayout(myMainActivity); //Set subgrid layout to match parent. PercentRelativeLayout.LayoutParams numFieldLayoutParms = new PercentRelativeLayout.LayoutParams( ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent ); //Get subgrid layout helper. PercentLayoutHelper.PercentLayoutInfo numFieldLayoutHelper = numFieldLayoutParms.PercentLayoutInfo; //Set left and top margins. numFieldLayoutHelper.LeftMarginPercent = ((yLoop % 3) * (1 / 3f)); numFieldLayoutHelper.TopMarginPercent = ((yLoop / 3) * (1 / 3f)); //Set subgrid container width. numFieldLayoutHelper.WidthPercent = (1 / 3f); //Set subgrid layout parms. numFieldLayout.LayoutParameters = numFieldLayoutParms; //Add frame to subgrid. subGridLayout.AddView(numFieldLayout); #endregion #region Number field layout: (SquarePercentLayout) //Create square percent layout for the num field. SquarePercentLayout numFieldHolder = new SquarePercentLayout(myMainActivity); //Set num field holder layout to match parent. PercentRelativeLayout.LayoutParams numfieldHolderParms = new PercentRelativeLayout.LayoutParams( ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent ); //Set subgrid layout parms. numFieldHolder.LayoutParameters = numfieldHolderParms; //Set subgrid padding to show small gridlines. numFieldHolder.SetPadding(2, 2, 2, 2); //Add num field holder to layout frame. numFieldLayout.AddView(numFieldHolder); #endregion #region Number field: (FitTextView) //Create num field. FitTextView numField = new FitTextView(myMainActivity); //Set numfield ID. numField.Id = idCounter; idCounter++; //Center num field. numField.Gravity = GravityFlags.Center; //Set numfield layout to match parent view. numField.LayoutParameters = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent ); //No font padding. numField.SetIncludeFontPadding(false); //Set so we don't lose numfield text on rotate. numField.FreezesText = true; //Set field text value. setNumText(numField); //Cells don't need focus because we are using our num picker. numField.Focusable = false; //Set callback for click to show our num picker. numField.Click += (object sender, EventArgs e) => { showNumPicker((FitTextView)sender); }; //Set field background color. numField.SetBackgroundColor(Android.Graphics.Color.DarkGreen); //Lay out field. numField.RequestLayout(); //Add field to holder. numFieldHolder.AddView(numField); #endregion } } }
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position) { RouteSegmentsViewHolder viewHolder = holder as RouteSegmentsViewHolder; RouteSegment[] segments = routeSegments[position]; viewHolder.From.Text = segments.First().DateFrom.ToString("HH:mm"); viewHolder.To.Text = segments.Last().DateTo.ToString("HH:mm"); viewHolder.Duration.Text = Math.Ceiling((segments.Last().DateTo - segments.First().DateFrom).TotalMinutes) + " min"; double left = Math.Ceiling((segments.First().DateFrom - DateTime.Now).TotalMinutes); viewHolder.Left.Text = left < 0 ? "Parti" : (left + Environment.NewLine + "min"); Context context = viewHolder.ItemView.Context; float density = context.Resources.DisplayMetrics.Density; DateTime begin = segments.First().DateFrom; DateTime end = segments.Last().DateTo; TimeSpan total = end - begin; viewHolder.Preview.RemoveAllViews(); PercentRelativeLayout.LayoutParams percentLayoutParams; LinearLayout.LayoutParams barBackLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent); barBackLayoutParams.SetMargins((int)(4 * density), 0, (int)(4 * density), 0); int i = 1; foreach (RouteSegment segment in segments) { Color color = Utils.GetColorForLine(context, segment.Line); ImageView image = new ImageView(context); image.Id = i; image.SetImageResource(Resource.Drawable.circle2); image.SetColorFilter(color); percentLayoutParams = new PercentRelativeLayout.LayoutParams((int)(14 * density), (int)(14 * density)); percentLayoutParams.AddRule(LayoutRules.CenterVertical); percentLayoutParams.SetMargins((int)(-7 * density), 0, 0, 0); percentLayoutParams.PercentLayoutInfo.LeftMarginPercent = (float)(segment.DateFrom - begin).Ticks / total.Ticks; viewHolder.Preview.AddView(image, percentLayoutParams); LinearLayout bar = new LinearLayout(context); View barBack = new View(context); barBack.SetBackgroundColor(color); bar.AddView(barBack, barBackLayoutParams); percentLayoutParams = new PercentRelativeLayout.LayoutParams((int)(28 * density), (int)(4 * density)); percentLayoutParams.AddRule(LayoutRules.CenterVertical); percentLayoutParams.AddRule(LayoutRules.RightOf, i); percentLayoutParams.SetMargins((int)(-7 * density), 0, (int)(-7 * density), 0); percentLayoutParams.PercentLayoutInfo.WidthPercent = (float)(segment.DateTo - segment.DateFrom).Ticks / total.Ticks; viewHolder.Preview.AddView(bar, percentLayoutParams); i++; } if (!viewHolders.Contains(viewHolder)) { viewHolders.Add(viewHolder); } }