예제 #1
0
        private void ResizeHeight(float height)
        {
            m_ScrollView.contentContainer.style.height = itemsSource.Count * itemHeight;

            // Restore scroll offset and pre-emptively update the highValue
            // in case this is the initial restore from persistent data and
            // the ScrollView's OnGeometryChanged() didn't update the low
            // and highValues.
            m_ScrollView.verticalScroller.highValue = Mathf.Max(m_ScrollOffset, m_ScrollView.verticalScroller.highValue);
            m_ScrollView.verticalScroller.value     = m_ScrollOffset;

            int itemCount = Math.Min((int)(height / itemHeight) + m_ExtraVisibleItems, itemsSource.Count);

            if (m_VisibleItemCount != itemCount)
            {
                if (m_VisibleItemCount > itemCount)
                {
                    // Shrink
                    int removeCount = m_VisibleItemCount - itemCount;
                    for (int i = 0; i < removeCount; i++)
                    {
                        m_Pool.RemoveAt(m_Pool.Count - 1);
                        m_ScrollView.RemoveAt(m_ScrollView.childCount - 1);
                    }
                }
                else
                {
                    // Grow
                    int addCount = itemCount - m_VisibleItemCount;
                    for (int i = 0; i < addCount; i++)
                    {
                        int index        = i + m_FirstVisibleIndex + m_VisibleItemCount;
                        var item         = makeItem();
                        var recycledItem = new RecycledItem(item);
                        m_Pool.Add(recycledItem);

                        item.style.marginTop     = 0;
                        item.style.marginBottom  = 0;
                        item.style.positionType  = PositionType.Absolute;
                        item.style.positionLeft  = 0;
                        item.style.positionRight = 0;
                        item.style.height        = itemHeight;
                        if (index < itemsSource.Count)
                        {
                            Setup(recycledItem, index);
                        }
                        else
                        {
                            item.style.visibility = Visibility.Hidden;
                        }

                        m_ScrollView.Add(item);
                    }
                }

                m_VisibleItemCount = itemCount;
            }

            m_LastHeight = height;
        }
예제 #2
0
        public void Refresh()
        {
            m_Pool.Clear();
            m_ScrollView.Clear();

            m_ScrollView.contentContainer.style.width = m_ScrollView.contentViewport.layout.width;
            m_ScrollView.contentContainer.style.flex  = 0;

            if (!HasValidDataAndBindings())
            {
                return;
            }

            // Resize ScrollView in case the collection has changed.
            m_ScrollView.contentContainer.style.height = itemsSource.Count * itemHeight;

            // Restore scroll offset and pre-emptively update the highValue
            // in case this is the initial restore from persistent data and
            // the ScrollView's OnGeometryChanged() didn't update the low
            // and highValues.
            m_ScrollView.verticalScroller.highValue = Mathf.Max(m_ScrollOffset, m_ScrollView.verticalScroller.highValue);
            m_ScrollView.verticalScroller.value     = m_ScrollOffset;

            if (m_LastSize != m_ScrollView.layout)
            {
                m_LastSize = m_ScrollView.layout;
            }

            if (float.IsNaN(m_LastSize.height))
            {
                return;
            }

            m_ScrollView.contentContainer.style.height = itemsSource.Count * itemHeight;

            m_VisibleItemCount = (int)(m_LastSize.height / itemHeight) + m_ExtraVisibleItems;
            for (var i = m_FirstVisibleIndex; i < m_VisibleItemCount + m_FirstVisibleIndex; i++)
            {
                var item         = makeItem();
                var recycledItem = new RecycledItem(item);
                m_Pool.Add(recycledItem);

                item.style.marginTop     = 0;
                item.style.marginBottom  = 0;
                item.style.positionType  = PositionType.Absolute;
                item.style.positionLeft  = 0;
                item.style.positionRight = 0;
                item.style.height        = itemHeight;
                if (i < itemsSource.Count)
                {
                    item.style.visibility = Visibility.Visible;
                    Setup(recycledItem, i);
                }
                else
                {
                    item.style.visibility = Visibility.Hidden;
                }

                m_ScrollView.Add(item);
            }

            schedule.Execute(() => { Dirty(ChangeType.Layout); });
        }