Пример #1
0
    protected void UpdateView()
    {
        if (!m_isValid)
        {
            return;
        }
        //计算view range
        Vector2 curContentPos = content.localPosition;
        int     minIndex      = -1;
        int     maxIndex      = -1;

        switch (m_layoutDirection)
        {
        case LayoutDirection.Horizontal:
            float minViewX = -curContentPos.x - m_layout.originSpace;
            float maxViewX = minViewX + m_viewportSize.x;
            minIndex = Mathf.CeilToInt(minViewX / (m_itemSize.x + m_layout.spacing)) - 1;
            maxIndex = Mathf.CeilToInt(maxViewX / (m_itemSize.x + m_layout.spacing)) - 1;
            break;

        case LayoutDirection.Vertical:
            float minViewY = curContentPos.y - m_layout.originSpace;
            float maxViewY = minViewY + m_viewportSize.y;
            minIndex = Mathf.CeilToInt(minViewY / (m_itemSize.y + m_layout.spacing)) - 1;
            maxIndex = Mathf.CeilToInt(maxViewY / (m_itemSize.y + m_layout.spacing)) - 1;
            break;
        }

        IndexRange viewRange = new IndexRange(minIndex, maxIndex);

        viewRange.Clamp(0, m_itemIds.Count - 1);
        //更新view
        if (viewRange.IsInRange(m_curContentRange))
        {
            return;
        }

        int dif = m_cacheitems.Count - viewRange.count;

        dif = Mathf.Max(0, dif);
        int startIdx = viewRange.minIndex - dif / 2;

        m_curContentRange = new IndexRange(startIdx, startIdx + m_cacheitems.Count - 1);
        m_curContentRange.Clamp(0, m_itemIds.Count - 1);
        showItems();
    }