Example #1
0
 private void UpdateSelected(int i)
 {
     if (i >= curVisibleStartIndex && i <= curVisibleEndIndex)
     {
         bool selected;
         if (!IsMultiSelect)
         {
             selected = SelectedIndex == i;
         }
         else
         {
             selected = MultiSelectedIndexs.Contains(i);
         }
         SetHighLight(_items[i - curVisibleStartIndex], selected);
     }
 }
Example #2
0
    void UpdateVisibleItem(bool forceUpdate)
    {
        int     newVisibleStartIndex;
        int     newVisibleEndIndex;
        Vector2 viewPortSize = _scroll.viewport.rect.size;
        Vector2 contentSize  = _scroll.content.rect.size;

        if (IsVertical)
        {
            float visibleStartY = (1 - _scroll.verticalNormalizedPosition) * (contentSize.y - viewPortSize.y);
            float visibleEndY   = visibleStartY + viewPortSize.y;

            newVisibleStartIndex = calcVisibleMinIndex(visibleStartY, itemSpace.y, Padding.top, _rowCellCount, itemSize.y);
            newVisibleEndIndex   = CalcVisibleMaxIndex(visibleEndY, itemSpace.y, Padding.top, _rowCellCount, itemSize.y);
        }
        else
        {
            float visibleStartX = (_scroll.horizontalNormalizedPosition) * (contentSize.x - viewPortSize.x);
            newVisibleStartIndex = Mathf.CeilToInt((visibleStartX + itemSpace.x - Padding.left + 0.1f) / (itemSize.x + itemSpace.x) - 1) * _colCellCount;
            float visibleEndX = visibleStartX + viewPortSize.x;
            newVisibleEndIndex = Mathf.CeilToInt((visibleEndX + itemSpace.x - Padding.left) / (itemSize.x + itemSpace.x)) * _colCellCount;

            newVisibleStartIndex = calcVisibleMinIndex(visibleStartX, itemSpace.x, Padding.left, _colCellCount, itemSize.x);
            newVisibleEndIndex   = CalcVisibleMaxIndex(visibleEndX, itemSpace.x, Padding.left, _colCellCount, itemSize.x);
        }
        newVisibleStartIndex = Mathf.Clamp(newVisibleStartIndex, 0, GetDataCount() - 1);
        newVisibleEndIndex   = Mathf.Clamp(newVisibleEndIndex, 0, GetDataCount() - 1);
        if (GetDataCount() == 0)
        {
            newVisibleStartIndex = 0;
            newVisibleEndIndex   = -1;
        }

        if (!forceUpdate && curVisibleStartIndex == newVisibleStartIndex && curVisibleEndIndex == newVisibleEndIndex)
        {
            return;
        }

        for (int i = curVisibleStartIndex; i <= curVisibleEndIndex; i++)
        {
            if (i < newVisibleStartIndex)
            {
                RectTransform item = _items[0];
                _items.Remove(item);
                RecycleItem(item);
            }
        }
        for (int i = curVisibleEndIndex; i >= curVisibleStartIndex; i--)
        {
            if (i > newVisibleEndIndex)
            {
                RectTransform item = _items[_items.Count - 1];
                _items.Remove(item);
                RecycleItem(item);
            }
        }
        for (int i = newVisibleStartIndex; i <= newVisibleEndIndex; i++)
        {
            if (i < 0)
            {
                continue;
            }
            if ((i < curVisibleStartIndex || i > curVisibleEndIndex))
            {
                RectTransform item = GetItemFromCache();
                item.transform.SetParent(_scroll.content, false);
                _items.Insert(i - newVisibleStartIndex, item);
                bool selected;
                if (IsMultiSelect)
                {
                    selected = _selectedIndex == i;
                }
                else
                {
                    selected = MultiSelectedIndexs.Contains(i);
                }
                OnChange(item, i);
                if (!forceUpdate)
                {
                    UpdateItemPos(item, i);
                }
            }
            if (forceUpdate)
            {
                OnChange(_items[i - newVisibleStartIndex], i);
                UpdateItemPos(_items[i - newVisibleStartIndex], i);
            }
        }
        curVisibleStartIndex = newVisibleStartIndex;
        curVisibleEndIndex   = newVisibleEndIndex;
        for (int i = curVisibleStartIndex; i <= curVisibleEndIndex; i++)
        {
            UpdateSelected(i);
        }
        if (onUpdateVisibleItem != null)
        {
            onUpdateVisibleItem(curVisibleStartIndex, curVisibleEndIndex);
        }
    }