Beispiel #1
0
        public void ShowElement(int x)
        {
            if (ItemsSource == null)
            {
                return;
            }
            var objs = new object[ItemsSource.Count];

            ItemsSource.CopyTo(objs, 0);

            var minX = x - CacheWidth;
            var maxX = x + CacheWidth;

            var countMinX = 0;
            var countMaxX = ItemsSource.Count - 1;

            if (ElementWidth > 0)
            {
                countMinX = minX / ElementWidth;
                countMaxX = maxX / ElementWidth - 1;
                countMinX = Math.Max(0, countMinX);
                countMaxX = Math.Min(ItemsSource.Count - 1, countMaxX);
            }

            // TODO optimize it
            for (int i = countMinX; i <= countMaxX; i++)
            {
                if (elements[i] != null)
                {
                    continue;
                }

                // Add view
                var o = CreateContent() as View;

                if (o != null)
                {
                    o.BindingContext = objs[i];
                    Children.Add(o, new Point((double)i * ElementWidth, 0.0));
                    elements[i] = o;

                    if (Added != null)
                    {
                        Added(i, objs[i]);
                    }
                }
            }

            for (int i = 0; i < elements.Count; i++)
            {
                if (countMinX <= i && i <= countMaxX)
                {
                    continue;
                }
                if (elements[i] == null)
                {
                    continue;
                }

                // Remove view
                Children.Remove(elements[i]);
                elements[i] = null;

                if (Removed != null)
                {
                    Removed(i, objs[i]);
                }
            }
        }