void CreateRecyclerView() { _recyclerView = new ScrollRecyclerView(Android.App.Application.Context); _recyclerView.Touch += _recyclerView_Touch; var scrollListener = new GridViewScrollListener(Element, _recyclerView); _recyclerView.AddOnScrollListener(scrollListener); if (Element.IsHorizontal) { var linearLayoutManager = new LinearLayoutManager(Context, OrientationHelper.Horizontal, false); _layoutManager = linearLayoutManager; } else { var gridlayoutManager = new GridLayoutManager(Context, 1); _layoutManager = gridlayoutManager; } _recyclerView.SetLayoutManager(_layoutManager); _recyclerView.SetItemAnimator(null); _recyclerView.HasFixedSize = true; _recyclerView.HorizontalScrollBarEnabled = Element.IsHorizontal; _recyclerView.VerticalScrollBarEnabled = !Element.IsHorizontal; _adapter = new GridViewAdapter(Element.ItemsSource, _recyclerView, Element, Resources.DisplayMetrics); _recyclerView.SetAdapter(_adapter); UpdatePadding(); }
/// <summary> /// Called when [element changed]. /// </summary> /// <param name="e">The e.</param> protected override void OnElementChanged(ElementChangedEventArgs <GridView> e) { base.OnElementChanged(e); var context = Xamarin.Forms.Forms.Context; var collectionView = new Android.Widget.GridView(context); collectionView.SetGravity(GravityFlags.Center); collectionView.SetColumnWidth(Convert.ToInt32(Element.ItemWidth)); collectionView.StretchMode = StretchMode.StretchColumnWidth; var metrics = Resources.DisplayMetrics; var spacing = (int)e.NewElement.ColumnSpacing; var width = metrics.WidthPixels; var itemWidth = (int)e.NewElement.ItemWidth; int noOfColumns = width / (itemWidth + spacing); // If possible add another row without spacing (because the number of columns will be one less than the number of spacings) if (width - (noOfColumns * (itemWidth + spacing)) >= itemWidth) { noOfColumns++; } collectionView.SetNumColumns(noOfColumns); collectionView.SetPadding(Convert.ToInt32(Element.Padding.Left), Convert.ToInt32(Element.Padding.Top), Convert.ToInt32(Element.Padding.Right), Convert.ToInt32(Element.Padding.Bottom)); collectionView.SetBackgroundColor(Element.BackgroundColor.ToAndroid()); collectionView.SetHorizontalSpacing(Convert.ToInt32(Element.RowSpacing)); collectionView.SetVerticalSpacing(Convert.ToInt32(Element.ColumnSpacing)); this.Unbind(e.OldElement); this.Bind(e.NewElement); _adapter = new GridViewAdapter(context, collectionView, Element); collectionView.Adapter = _adapter; collectionView.ItemClick += CollectionViewItemClick; base.SetNativeControl(collectionView); }