Ejemplo n.º 1
0
        void OnChildAdded(object sender, ElementEventArgs e)
        {
            var view = e.Element as VisualElement;

            if (view != null)
            {
                AddChild(view);
            }

            int itemCount = ElementController.LogicalChildren.Count;

            if (itemCount <= 1)
            {
                return;
            }

            Element lastChild = ElementController.LogicalChildren[itemCount - 1];

            if (lastChild != view)
            {
                EnsureChildOrder();
                return;
            }

            if (!Forms.IsLollipopOrNewer)
            {
                return;
            }

            Element previousChild = ElementController.LogicalChildren[itemCount - 2];

            IVisualElementRenderer lastRenderer     = null;
            IVisualElementRenderer previousRenderer = null;

            if (lastChild is VisualElement last)
            {
                lastRenderer = AppCompat.Platform.GetRenderer(last);
            }

            if (previousChild is VisualElement previous)
            {
                previousRenderer = AppCompat.Platform.GetRenderer(previous);
            }

            if (ElevationHelper.GetElevation(lastRenderer?.View) < ElevationHelper.GetElevation(previousRenderer?.View))
            {
                EnsureChildOrder();
            }
        }
Ejemplo n.º 2
0
        void EnsureChildOrder(bool onlyUpdateElevations)
        {
            float elevationToSet = 0;

            for (var i = 0; i < ElementController.LogicalChildren.Count; i++)
            {
                Element child   = ElementController.LogicalChildren[i];
                var     element = (VisualElement)child;
                if (element != null)
                {
                    IVisualElementRenderer r = AppCompat.Platform.GetRenderer(element);
                    if (r != null)
                    {
                        if (Forms.IsLollipopOrNewer)
                        {
                            var elevation        = ElevationHelper.GetElevation(r.View) ?? 0;
                            var elementElevation = ElevationHelper.GetElevation(element, r.View.Context);

                            if (elementElevation == null)
                            {
                                if (elevation > elevationToSet)
                                {
                                    elevationToSet = elevation;
                                }

                                if (r.View.Elevation != elevationToSet)
                                {
                                    r.View.Elevation = elevationToSet;
                                }
                            }
                        }

                        if (!onlyUpdateElevations)
                        {
                            (_renderer.View as ViewGroup)?.BringChildToFront(r.View);
                        }
                    }
                }
            }
        }