Example #1
0
        public override void RequestLayout(float scrollPosition, bool force = false)
        {
            // Layouting is only possible after once it initialized.
            if (!IsInitialized)
            {
                return;
            }
            int LastIndex = colView.InternalItemSource.Count - 1;

            if (!force && PrevScrollPosition == Math.Abs(scrollPosition))
            {
                return;
            }
            PrevScrollPosition = Math.Abs(scrollPosition);

            if (ItemSizeChanged >= 0)
            {
                for (int i = ItemSizeChanged; i <= LastIndex; i++)
                {
                    UpdatePosition(i);
                }
                ScrollContentSize = ItemPosition[LastIndex - 1] + GetItemSize(LastIndex);
            }

            int prevFirstVisible = FirstVisible;
            int prevLastVisible  = LastVisible;

            (float X, float Y)visibleArea = (PrevScrollPosition,
                                             PrevScrollPosition + (IsHorizontal ? colView.Size.Width : colView.Size.Height)
                                             );

            // 1. Set First/Last Visible Item Index.
            (int start, int end) = FindVisibleItems(visibleArea);
            FirstVisible         = start;
            LastVisible          = end;

            // 2. Unrealize invisible items.
            List <RecyclerViewItem> unrealizedItems = new List <RecyclerViewItem>();

            foreach (RecyclerViewItem item in VisibleItems)
            {
                if (item.Index < FirstVisible || item.Index > LastVisible)
                {
                    //Console.WriteLine("[NUI] Unrealize{0}!", item.Index);
                    unrealizedItems.Add(item);
                    colView.UnrealizeItem(item);
                }
            }
            VisibleItems.RemoveAll(unrealizedItems.Contains);

            // 3. Realize and placing visible items.
            for (int i = FirstVisible; i <= LastVisible; i++)
            {
                RecyclerViewItem item = null;
                // 4. Get item if visible or realize new.
                if (i >= prevFirstVisible && i <= prevLastVisible)
                {
                    item = GetVisibleItem(i);
                    if (item)
                    {
                        continue;
                    }
                }
                if (item == null)
                {
                    item = colView.RealizeItem(i);
                }

                VisibleItems.Add(item);

                // 5. Placing item.
                float posX = 0F, posY = 0F;
                if (isGrouped)
                {
                    //isHeader?
                    if (colView.Header == item)
                    {
                        posX = 0F;
                        posY = 0F;
                    }
                    else if (colView.Footer == item)
                    {
                        posX = (IsHorizontal ? ScrollContentSize - item.SizeWidth : 0F);
                        posY = (IsHorizontal ? 0F : ScrollContentSize - item.SizeHeight);
                    }
                    else
                    {
                        GroupInfo gInfo = GetGroupInfo(i);
                        posX = (IsHorizontal ? gInfo.GroupPosition + gInfo.ItemPosition[i - gInfo.StartIndex] : 0F);
                        posY = (IsHorizontal ? 0F : gInfo.GroupPosition + gInfo.ItemPosition[i - gInfo.StartIndex]);
                    }
                }
                else
                {
                    posX = (IsHorizontal ? ItemPosition[i] : 0F);
                    posY = (IsHorizontal ? 0F : ItemPosition[i]);
                }

                item.Position = new Position(posX, posY);
                //Console.WriteLine("[NUI] ["+item+"]["+item.Index+"] :: ["+item.Position.X+", "+item.Position.Y+"] ==== \n");
            }
        }
Example #2
0
        /// <summary>
        /// This is called to find out where items are lain out according to current scroll position.
        /// </summary>
        /// <param name="scrollPosition">Scroll position which is calculated by ScrollableBase</param>
        /// <param name="force">boolean force flag to layouting forcely.</param>
        public override void RequestLayout(float scrollPosition, bool force = false)
        {
            // Layouting is only possible after once it intialized.
            if (!IsInitialized)
            {
                return;
            }
            int LastIndex = colView.InternalItemSource.Count;

            if (!force && PrevScrollPosition == Math.Abs(scrollPosition))
            {
                return;
            }
            PrevScrollPosition = Math.Abs(scrollPosition);

            int  prevFirstVisible = FirstVisible;
            int  prevLastVisible  = LastVisible;
            bool IsHorizontal     = (colView.ScrollingDirection == ScrollableBase.Direction.Horizontal);

            (float X, float Y)visibleArea = (PrevScrollPosition,
                                             PrevScrollPosition + (IsHorizontal ? colView.Size.Width : colView.Size.Height)
                                             );

            //Console.WriteLine("[NUI] itemsView [{0},{1}] [{2},{3}]", colView.Size.Width, colView.Size.Height, colView.ContentContainer.Size.Width, colView.ContentContainer.Size.Height);

            // 1. Set First/Last Visible Item Index.
            (int start, int end) = FindVisibleItems(visibleArea);
            FirstVisible         = start;
            LastVisible          = end;

            //Console.WriteLine("[NUI] {0} :visibleArea before [{1},{2}] after [{3},{4}]", scrollPosition, prevFirstVisible, prevLastVisible, FirstVisible, LastVisible);

            // 2. Unrealize invisible items.
            List <RecyclerViewItem> unrealizedItems = new List <RecyclerViewItem>();

            foreach (RecyclerViewItem item in VisibleItems)
            {
                if (item.Index < FirstVisible || item.Index > LastVisible)
                {
                    //Console.WriteLine("[NUI] Unrealize{0}!", item.Index);
                    unrealizedItems.Add(item);
                    colView.UnrealizeItem(item);
                }
            }
            VisibleItems.RemoveAll(unrealizedItems.Contains);

            //Console.WriteLine("Realize Begin [{0} to {1}]", FirstVisible, LastVisible);
            // 3. Realize and placing visible items.
            for (int i = FirstVisible; i <= LastVisible; i++)
            {
                //Console.WriteLine("[NUI] Realize!");
                RecyclerViewItem item = null;
                // 4. Get item if visible or realize new.
                if (i >= prevFirstVisible && i <= prevLastVisible)
                {
                    item = GetVisibleItem(i);
                    if (item)
                    {
                        continue;
                    }
                }
                if (item == null)
                {
                    item = colView.RealizeItem(i);
                }
                VisibleItems.Add(item);

                (float x, float y) = GetItemPosition(i);
                // 5. Placing item.
                item.Position = new Position(x, y);
                //Console.WriteLine("[NUI] ["+item.Index+"] ["+item.Position.X+", "+item.Position.Y+" ==== \n");
            }
            //Console.WriteLine("Realize Done");
        }