Esempio n. 1
0
        void CheckAndShowItem(int criticalItemType)
        {
            ScrollItem item          = null;
            int        criticalIndex = -1;

            while (true)
            {
                item          = GetCriticalItem(criticalItemType);
                criticalIndex = criticalItemIndex[criticalItemType];

                //if (item == null && ShouldItemFullySeenAtIndex(criticalItemIndex[criticalItemType - 2]))

                if (item == null && ShouldItemSeenAtIndex(criticalIndex))
                {
                    ScrollItem newItem = itemPool.Get();
                    OnGetItemForDataIndex(newItem, criticalIndex);
                    //Debug.Log("创建了 " + criticalIndex);
                    managedItems[criticalIndex].item = newItem;

                    criticalItemIndex[criticalItemType - 2] = criticalIndex;

                    if (criticalItemType == CriticalItemType.UpToShow)
                    {
                        // 最上显示了一个
                        criticalItemIndex[criticalItemType]--;
                    }
                    else
                    {
                        // 最下显示了一个
                        criticalItemIndex[criticalItemType]++;
                    }
                    criticalItemIndex[criticalItemType] = Mathf.Clamp(criticalItemIndex[criticalItemType], 0, m_dataCount - 1);
                }
                else
                {
                    break;
                }
            }
        }
Esempio n. 2
0
        private RectTransform GetNewItem(int index)
        {
            RectTransform item;

            if (itemGetFunc != null)
            {
                item = itemGetFunc(index);
            }
            else
            {
                item = itemPool.Get();
            }
            return(item);
        }
Esempio n. 3
0
        public void UpdateData()
        {
            int newDataCount = 0;

            if (itemCoundFunc != null)
            {
                newDataCount = itemCoundFunc();
            }

            if (newDataCount != managedItems.Length)
            {
                if (newDataCount > managedItems.Length)  //增加
                {
                    Array.Resize(ref managedItems, newDataCount);
                }
                else //减少
                {
                    for (int i = newDataCount; i < m_dataCount; i++)
                    {
                        if (managedItems[i].item != null)
                        {
                            itemPool.Recycle(managedItems[i].item);
                            managedItems[i].item = null;
                        }
                    }
                    //Array.Resize(ref managedItems, newDataCount); //减少时保留之前多余的空位
                }
                m_dataCount = newDataCount;
            }

            CacheRect();

            int showCount = Mathf.Min(maxShownCount, m_dataCount);
            int restCount = showCount;

            bool hasItem, shouldShow;
            int  firstIndex = -1, lastIndex = -1;

            for (int i = 0; i < m_dataCount && restCount > 0; i++)
            {
                hasItem    = managedItems[i].item != null;
                shouldShow = ShouldItemSeenAtIndex(i);

                if (shouldShow)
                {
                    restCount--;
                    if (firstIndex == -1)
                    {
                        firstIndex = i;
                    }
                    lastIndex = i;
                }

                if (hasItem && shouldShow)
                {
                    // 应显示且已显示
                    SetDataForItemAtIndex(managedItems[i].item, i);
                    continue;
                }

                if (hasItem == shouldShow)
                {
                    // 不应显示且未显示
                    continue;
                }

                if (hasItem && !shouldShow)
                {
                    // 不该显示 但是有
                    itemPool.Recycle(managedItems[i].item);
                    managedItems[criticalItemIndex[i]].item = null;
                    continue;
                }

                if (shouldShow && !hasItem)
                {
                    // 需要显示 但是没有
                    RectTransform item = itemPool.Get();
                    OnGetItemForDataIndex(item, i);
                    managedItems[i].item = item;
                    continue;
                }
            }

            // content.localPosition = Vector2.zero;
            criticalItemIndex[CriticalItemType.UpToHide]   = firstIndex;
            criticalItemIndex[CriticalItemType.DownToHide] = lastIndex;
            criticalItemIndex[CriticalItemType.UpToShow]   = Mathf.Max(firstIndex - 1, 0);
            criticalItemIndex[CriticalItemType.DownToShow] = Mathf.Min(lastIndex + 1, m_dataCount - 1);
        }
        private void InternalUpdateData()
        {
            willUpdateData = false;

            int newDataCount = 0;

            if (itemCoundFunc != null)
            {
                newDataCount = itemCoundFunc();
            }

            if (newDataCount != managedItems.Count)
            {
                if (managedItems.Count < newDataCount) //增加
                {
                    foreach (var itemWithRect in managedItems)
                    {
                        // 重置所有rect
                        itemWithRect.rectDirty = true;
                    }
                    while (managedItems.Count < newDataCount)
                    {
                        managedItems.Add(new ScrollItemWithRect());
                    }
                }
                else //减少 保留空位 避免GC
                {
                    for (int i = 0, count = managedItems.Count; i < count; ++i)
                    {
                        // 重置所有rect
                        managedItems[i].rectDirty = true;

                        // 超出部分 清理回收item
                        if (i >= newDataCount)
                        {
                            if (managedItems[i].item != null)
                            {
                                itemPool.Recycle(managedItems[i].item);
                                managedItems[i].item = null;
                            }
                        }
                    }
                }
            }

            m_dataCount = newDataCount;

            // CacheRect();

            int showCount = Mathf.Min(maxShownCount, m_dataCount);
            int restCount = showCount;

            bool hasItem, shouldShow;
            int  firstIndex = -1, lastIndex = -1;

            for (int i = 0; i < m_dataCount && restCount > 0; i++)
            {
                hasItem    = managedItems[i].item != null;
                shouldShow = ShouldItemSeenAtIndex(i);

                if (shouldShow)
                {
                    restCount--;
                    if (firstIndex == -1)
                    {
                        firstIndex = i;
                    }
                    lastIndex = i;
                }

                if (hasItem && shouldShow)
                {
                    // 应显示且已显示
                    SetDataForItemAtIndex(managedItems[i].item, i);
                    continue;
                }

                if (hasItem == shouldShow)
                {
                    // 不应显示且未显示
                    if (firstIndex != -1)
                    {
                        // 已经遍历完所有要显示的了 后边的先跳过
                        break;
                    }
                    continue;
                }

                if (hasItem && !shouldShow)
                {
                    // 不该显示 但是有
                    itemPool.Recycle(managedItems[i].item);
                    managedItems[i].item = null;
                    continue;
                }

                if (shouldShow && !hasItem)
                {
                    // 需要显示 但是没有
                    RectTransform item = itemPool.Get();
                    OnGetItemForDataIndex(item, i);
                    managedItems[i].item = item;
                    continue;
                }
            }

            // content.localPosition = Vector2.zero;
            criticalItemIndex[CriticalItemType.UpToHide]   = firstIndex;
            criticalItemIndex[CriticalItemType.DownToHide] = lastIndex;
            criticalItemIndex[CriticalItemType.UpToShow]   = Mathf.Max(firstIndex - 1, 0);
            criticalItemIndex[CriticalItemType.DownToShow] = Mathf.Min(lastIndex + 1, m_dataCount - 1);
        }
Esempio n. 5
0
 public static Dictionary <T, U> Get()
 {
     return(pool.Get());
 }
Esempio n. 6
0
 public static List <T> Get()
 {
     return(pool.Get());
 }