SetViewPager() public method

public SetViewPager ( ViewPager view ) : void
view ViewPager
return void
示例#1
0
        async void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // NewItems contains the item that was added.
            // If NewStartingIndex is not -1, then it contains the index where the new item was added.
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                InsertPage(Element?.ItemsSource.GetItem(e.NewStartingIndex), e.NewStartingIndex);
            }

            // OldItems contains the item that was removed.
            // If OldStartingIndex is not -1, then it contains the index where the old item was removed.
            if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                await RemovePage(e.OldStartingIndex);
            }

            // OldItems contains the moved item.
            // OldStartingIndex contains the index where the item was moved from.
            // NewStartingIndex contains the index where the item was moved to.
            if (e.Action == NotifyCollectionChangedAction.Move)
            {
                var Source = ((PageAdapter)viewPager?.Adapter).Source;

                if (Element != null && viewPager != null && Source != null)
                {
                    Source.RemoveAt(e.OldStartingIndex);
                    Source.Insert(e.NewStartingIndex, e.OldItems[e.OldStartingIndex]);
                    viewPager.Adapter?.NotifyDataSetChanged();

                    Element.PositionSelected?.Invoke(Element, Element.Position);
                }
            }

            // NewItems contains the replacement item.
            // NewStartingIndex and OldStartingIndex are equal, and if they are not -1,
            // then they contain the index where the item was replaced.
            if (e.Action == NotifyCollectionChangedAction.Replace)
            {
                var Source = ((PageAdapter)viewPager?.Adapter).Source;

                if (Element != null && viewPager != null && Source != null)
                {
                    Source[e.OldStartingIndex] = e.NewItems[e.NewStartingIndex];
                    viewPager.Adapter?.NotifyDataSetChanged();
                }
            }

            // No other properties are valid.
            if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                if (Element != null && viewPager != null)
                {
                    SetPosition();
                    viewPager.Adapter = new PageAdapter(Element);
                    viewPager.SetCurrentItem(Element.Position, false);
                    indicators?.SetViewPager(viewPager);
                    Element.PositionSelected?.Invoke(Element, Element.Position);
                }
            }
        }
示例#2
0
        void SetIndicators()
        {
            if (Element.ShowIndicators)
            {
                SetPosition();

                indicators.SetViewPager(viewPager);

                // IndicatorsTintColor BP
                indicators.SetFillColor(Element.IndicatorsTintColor.ToAndroid());

                // CurrentPageIndicatorTintColor BP
                indicators.SetPageColor(Element.CurrentPageIndicatorTintColor.ToAndroid());

                // IndicatorsShape BP
                indicators.SetStyle(Element.IndicatorsShape); // Rounded or Squared
            }
            else
            {
                indicators.RemoveAllViews();
            }

            // ShowIndicators BP
            indicators.Visibility = Element.ShowIndicators ? AViews.ViewStates.Visible : AViews.ViewStates.Gone;
        }
        void SetIndicators()
        {
            if (Element.ShowIndicators)
            {
                SetPosition();

                indicators.SetViewPager(viewPager);

                // IndicatorsTintColor BP
                indicators.SetFillColor(Element.IndicatorsTintColor.ToAndroid());

                // CurrentPageIndicatorTintColor BP
                indicators.SetPageColor(Element.CurrentPageIndicatorTintColor.ToAndroid());

                // IndicatorsShape BP
                indicators.SetStyle(Element.IndicatorsShape);                 // Rounded or Squared

                var layoutParams = new global::Android.Widget.RelativeLayout.LayoutParams(global::Android.Widget.RelativeLayout.LayoutParams.MatchParent, global::Android.Widget.RelativeLayout.LayoutParams.WrapContent);
                layoutParams.AddRule(Element.IndicatorLocation == IndicatorLocation.Top ? global::Android.Widget.LayoutRules.AlignParentTop : global::Android.Widget.LayoutRules.AlignParentBottom);
                indicators.LayoutParameters = layoutParams;
            }
            else
            {
                indicators.RemoveAllViews();
            }

            // ShowIndicators BP
            indicators.Visibility = Element.ShowIndicators? AViews.ViewStates.Visible : AViews.ViewStates.Gone;
        }
        void SetNativeView()
        {
            var inflater = AViews.LayoutInflater.From(Forms.Context);

            // Orientation BP
            if (Element.Orientation == CarouselViewOrientation.Horizontal)
            {
                nativeView = inflater.Inflate(Resource.Layout.horizontal_viewpager, null);
            }
            else
            {
                nativeView = inflater.Inflate(Resource.Layout.vertical_viewpager, null);
            }

            viewPager = nativeView.FindViewById <ViewPager>(Resource.Id.pager);

            viewPager.Adapter = new PageAdapter(Element);
            viewPager.SetCurrentItem(Element.Position, false);

            // InterPageSpacing BP
            var metrics          = Resources.DisplayMetrics;
            var interPageSpacing = Element.InterPageSpacing * metrics.Density;

            viewPager.PageMargin = (int)interPageSpacing;

            // InterPageSpacingColor BP
            viewPager.SetBackgroundColor(Element.InterPageSpacingColor.ToAndroid());

            // IsSwipingEnabled BP
            SetIsSwipingEnabled();

            // INDICATORS
            indicators = nativeView.FindViewById <CirclePageIndicator>(Resource.Id.indicator);

            SetPosition();

            indicators.SetViewPager(viewPager);

            // IndicatorsTintColor BP
            indicators.SetFillColor(Element.IndicatorsTintColor.ToAndroid());

            // CurrentPageIndicatorTintColor BP
            indicators.SetPageColor(Element.CurrentPageIndicatorTintColor.ToAndroid());

            // IndicatorsShape BP
            indicators.SetStyle(Element.IndicatorsShape);             // Rounded or Squared

            // ShowIndicators BP
            indicators.Visibility = Element.ShowIndicators ? AViews.ViewStates.Visible : AViews.ViewStates.Gone;

            viewPager.PageSelected           += ViewPager_PageSelected;
            viewPager.PageScrollStateChanged += ViewPager_PageScrollStateChanged;

            SetNativeControl(nativeView);
        }
        protected override void OnElementChanged(ElementChangedEventArgs <CarouselViewControl> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                // Instantiate the native control and assign it to the Control property with
                // the SetNativeControl method

                var inflater = AViews.LayoutInflater.From(Forms.Context);

                if (Element.Orientation == Orientation.Horizontal)
                {
                    nativeView = inflater.Inflate(Resource.Layout.viewpager, null);
                }
                else
                {
                    nativeView = inflater.Inflate(Resource.Layout.vertical_viewpager, null);
                }

                var metrics          = Resources.DisplayMetrics;
                var interPageSpacing = Element.InterPageSpacing * metrics.Density;

                viewPager            = nativeView.FindViewById <ViewPager>(Resource.Id.pager);
                viewPager.PageMargin = (int)interPageSpacing;

                indicator = nativeView.FindViewById <CirclePageIndicator>(Resource.Id.indicator);
                indicator.SetViewPager(viewPager);

                indicator.SetPageColor(Element.PageIndicatorTintColor.ToAndroid());
                indicator.SetFillColor(Element.CurrentPageIndicatorTintColor.ToAndroid());

                indicator.Visibility = Element.ShowIndicators ? AViews.ViewStates.Visible : AViews.ViewStates.Gone;

                SetNativeControl(nativeView);
            }

            if (e.OldElement != null)
            {
                // Unsubscribe from event handlers and cleanup any resources

                if (viewPager != null)
                {
                    viewPager.PageSelected           -= ViewPager_PageSelected;
                    viewPager.PageScrollStateChanged -= ViewPager_PageScrollStateChanged;
                }

                if (Element != null)
                {
                    Element.RemoveAction     = null;
                    Element.InsertAction     = null;
                    Element.SetCurrentAction = null;
                }
            }

            if (e.NewElement != null)
            {
                // Configure the control and subscribe to event handlers

                viewPager.PageSelected           += ViewPager_PageSelected;
                viewPager.PageScrollStateChanged += ViewPager_PageScrollStateChanged;

                Element.RemoveAction     = new Action <int>(RemoveItem);
                Element.InsertAction     = new Action <object, int>(InsertItem);
                Element.SetCurrentAction = new Action <int>(SetCurrentItem);
            }
        }
        async void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // NewItems contains the item that was added.
            // If NewStartingIndex is not -1, then it contains the index where the new item was added.
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                InsertPage(Element?.ItemsSource.GetItem(e.NewStartingIndex), e.NewStartingIndex);
            }

            // OldItems contains the item that was removed.
            // If OldStartingIndex is not -1, then it contains the index where the old item was removed.
            if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                await RemovePage(e.OldStartingIndex);
            }

            // OldItems contains the moved item.
            // OldStartingIndex contains the index where the item was moved from.
            // NewStartingIndex contains the index where the item was moved to.
            if (e.Action == NotifyCollectionChangedAction.Move)
            {
                // Fix for #168 Android NullReferenceException
                var Source = ((PageAdapter)viewPager?.Adapter)?.Source;

                if (Element == null || viewPager == null || viewPager?.Adapter == null || Source == null)
                {
                    return;
                }

                Source.RemoveAt(e.OldStartingIndex);
                Source.Insert(e.NewStartingIndex, e.OldItems[e.OldStartingIndex]);
                viewPager.Adapter?.NotifyDataSetChanged();

                SetArrowsVisibility();

                Element.SendPositionSelected();
                Element.PositionSelectedCommand?.Execute(null);
            }

            // NewItems contains the replacement item.
            // NewStartingIndex and OldStartingIndex are equal, and if they are not -1,
            // then they contain the index where the item was replaced.
            if (e.Action == NotifyCollectionChangedAction.Replace)
            {
                // Fix for #168 Android NullReferenceException
                var Source = ((PageAdapter)viewPager?.Adapter)?.Source;

                if (Element == null || viewPager == null || viewPager?.Adapter == null || Source == null)
                {
                    return;
                }

                Source[e.OldStartingIndex] = e.NewItems[e.NewStartingIndex];
                viewPager.Adapter?.NotifyDataSetChanged();
            }

            // No other properties are valid.
            if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                if (Element == null || viewPager == null)
                {
                    return;
                }

                SetPosition();
                viewPager.Adapter = new PageAdapter(Element);
                viewPager.SetCurrentItem(Element.Position, false);
                SetArrowsVisibility();
                indicators?.SetViewPager(viewPager);
                Element.SendPositionSelected();
                Element.PositionSelectedCommand?.Execute(null);
            }
        }
        async void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // NewItems contains the item that was added.
            // If NewStartingIndex is not -1, then it contains the index where the new item was added.
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                InsertPage(Element?.ItemsSource.GetItem(e.NewStartingIndex), e.NewStartingIndex);
            }

            // OldItems contains the item that was removed.
            // If OldStartingIndex is not -1, then it contains the index where the old item was removed.
            if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                await RemovePageAsync(e.OldStartingIndex);
            }

            // OldItems contains the moved item.
            // OldStartingIndex contains the index where the item was moved from.
            // NewStartingIndex contains the index where the item was moved to.
            if (e.Action == NotifyCollectionChangedAction.Move)
            {
                // Fix for #168 Android NullReferenceException
                var Source = ((PageAdapter)viewPager?.Adapter)?.Source;

                if (Element != null && viewPager != null && viewPager?.Adapter != null && Source != null)
                {
                    /*
                     * System.IndexOutOfRangeException: Index has to be between upper and lower bound of the array.
                     * I/MonoDroid(24479):   at System.Array.GetValue (System.Int32 index) [0x00037] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
                     * I/MonoDroid(24479):   at System.Array.System.Collections.IList.get_Item (System.Int32 index) [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
                     * I/MonoDroid(24479):   at System.Collections.ArrayList+ReadOnlyList.get_Item (System.Int32 index) [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
                     * I/MonoDroid(24479):   at CarouselView.FormsPlugin.Android.CarouselViewRenderer+<ItemsSource_CollectionChanged>d__6.MoveNext () [0x0018c] in .\CarouselView\CarouselView.FormsPlugin.Android\CarouselViewImplementation.cs:114
                     */
                    //Source.RemoveAt(e.OldStartingIndex);
                    //Source.Insert(e.NewStartingIndex, e.OldItems[e.OldStartingIndex]);
                    //viewPager.Adapter?.NotifyDataSetChanged();
                    //Element.SendPositionSelected();

                    var maxIndex = Source.Count - 1;

                    if (e.OldStartingIndex >= 0 && e.OldStartingIndex <= maxIndex &&
                        e.NewStartingIndex >= 0 && e.NewStartingIndex <= maxIndex)
                    {
                        var item = Source.ElementAtOrDefault(e.OldStartingIndex);
                        if (item != null)
                        {
                            await RemovePageAsync(e.OldStartingIndex, false);

                            InsertPage(item, e.NewStartingIndex);
                            await SetCurrentPageAsync(e.NewStartingIndex);

                            Element.SendPositionSelected();
                        }
                    }
                }
            }

            // NewItems contains the replacement item.
            // NewStartingIndex and OldStartingIndex are equal, and if they are not -1,
            // then they contain the index where the item was replaced.
            if (e.Action == NotifyCollectionChangedAction.Replace)
            {
                // Fix for #168 Android NullReferenceException
                var Source = ((PageAdapter)viewPager?.Adapter)?.Source;

                if (Element != null && viewPager != null && viewPager?.Adapter != null && Source != null)
                {
                    Source[e.OldStartingIndex] = e.NewItems[e.NewStartingIndex];
                    viewPager.Adapter?.NotifyDataSetChanged();
                }
            }

            // No other properties are valid.
            if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                if (Element != null && viewPager != null)
                {
                    SetPosition();
                    viewPager.Adapter = new PageAdapter(Element);
                    viewPager.SetCurrentItem(Element.Position, false);
                    indicators?.SetViewPager(viewPager);
                    Element.SendPositionSelected();
                }
            }
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            var rect = this.Element.Bounds;

            switch (e.PropertyName)
            {
            case "Renderer":
                // Fix for NullReferenceException on Android tabbed page #59
                if (!Element.Height.Equals(-1))
                {
                    SetNativeView();
                    Element.PositionSelected?.Invoke(Element, EventArgs.Empty);
                }
                break;

            case "Width":
                //ElementWidth = rect.Width;
                break;

            case "Height":
                // To avoid calling ConfigureViewPager twice on launch;
                if (ElementHeight.Equals(0))
                {
                    ElementHeight = rect.Height;
                    SetNativeView();
                    Element.PositionSelected?.Invoke(Element, EventArgs.Empty);
                }
                break;

            case "Orientation":
                SetNativeView();
                Element.PositionSelected?.Invoke(Element, EventArgs.Empty);
                break;

            case "InterPageSpacing":
                //var metrics = Resources.DisplayMetrics;
                //var interPageSpacing = Element.InterPageSpacing * metrics.Density;
                //viewPager.PageMargin = (int)interPageSpacing;
                break;

            case "InterPageSpacingColor":
                viewPager?.SetBackgroundColor(Element.InterPageSpacingColor.ToAndroid());
                break;

            case "IsSwipingEnabled":
                SetIsSwipingEnabled();
                break;

            case "IndicatorsTintColor":
                indicators?.SetFillColor(Element.IndicatorsTintColor.ToAndroid());
                break;

            case "CurrentPageIndicatorTintColor":
                indicators?.SetPageColor(Element.CurrentPageIndicatorTintColor.ToAndroid());
                break;

            case "IndicatorsShape":
                indicators?.SetStyle(Element.IndicatorsShape);
                break;

            case "ShowIndicators":
                if (indicators != null)
                {
                    indicators.Visibility = Element.ShowIndicators ? AViews.ViewStates.Visible : AViews.ViewStates.Gone;
                }
                break;

            case "ItemsSource":
                if (viewPager != null)
                {
                    SetPosition();
                    viewPager.Adapter = new PageAdapter(Element);
                    viewPager.SetCurrentItem(Element.Position, false);
                    indicators?.SetViewPager(viewPager);
                    Element.PositionSelected?.Invoke(Element, EventArgs.Empty);
                    if (Element.ItemsSource != null && Element.ItemsSource is INotifyCollectionChanged)
                    {
                        ((INotifyCollectionChanged)Element.ItemsSource).CollectionChanged += ItemsSource_CollectionChanged;
                    }
                }
                break;

            case "ItemTemplate":
                if (viewPager != null)
                {
                    viewPager.Adapter = new PageAdapter(Element);
                    viewPager.SetCurrentItem(Element.Position, false);
                    indicators?.SetViewPager(viewPager);
                    Element.PositionSelected?.Invoke(Element, EventArgs.Empty);
                }
                break;

            case "Position":
                if (!isSwiping)
                {
                    SetCurrentPage(Element.Position);
                }
                break;
            }
        }