예제 #1
0
        public void ItemSelected(ListItemBase item, bool clear = false)
        {
            if (onItemSelected != null)
            {
                onItemSelected(item);

                if (clear)
                {
                    onItemSelected = null;
                }
            }
        }
예제 #2
0
        private ListItemBase CreateNewItem(ListItemBase prefab, int index, float dimension)
        {
            GameObject instance = (GameObject)Instantiate(prefab.gameObject, Vector3.zero, Quaternion.identity);

            instance.transform.SetParent(_content.transform);
            instance.transform.localScale = Vector3.one;
            instance.SetActive(true);

            float position = index * (dimension + _spacing) + dimension / 2;

            RectTransform rectTransform = instance.GetComponent <RectTransform> ();


            rectTransform.anchorMin        = new Vector2(0, 1);
            rectTransform.anchorMax        = new Vector2(1, 1);
            rectTransform.anchoredPosition = new Vector2(0, -position);
            rectTransform.offsetMin        = new Vector2(0, rectTransform.offsetMin.y);
            rectTransform.offsetMax        = new Vector2(0, rectTransform.offsetMax.y);

            return(instance.GetComponent <ListItemBase> ());
        }
예제 #3
0
        private void RecycleItem(ScrollDirection direction)
        {
            ListItemBase firstItem = _itemsList [0];
            ListItemBase lastItem  = _itemsList [_itemsList.Count - 1];

            float targetPosition = (_itemSize + _spacing);

            switch (direction)
            {
            case ScrollDirection.NEXT:
                firstItem.Position = new Vector2(firstItem.Position.x, lastItem.Position.y - targetPosition);
                firstItem.Index    = _lastItemIndex;
                firstItem.transform.SetAsLastSibling();

                _itemsList.RemoveAt(0);
                _itemsList.Add(firstItem);

                ItemLoaded(firstItem);
                break;

            case ScrollDirection.PREVIOUS:


                lastItem.Position = new Vector2(lastItem.Position.x, firstItem.Position.y + targetPosition);

                lastItem.Index = _lastItemIndex - _itemsList.Count;
                lastItem.transform.SetAsFirstSibling();

                _itemsList.RemoveAt(_itemsList.Count - 1);
                _itemsList.Insert(0, lastItem);

                ItemLoaded(lastItem);
                break;
            }

            Canvas.ForceUpdateCanvases();
        }
예제 #4
0
 void HandleOnSelectedHandler(ListItemBase item)
 {
     ItemSelected(item);
 }
예제 #5
0
        public void Create(int items, ListItemBase listItemPrefab)
        {
            _scrollRect.vertical   = true;
            _scrollRect.horizontal = false;

            _content.anchorMin = new Vector2(0, 1);
            _content.anchorMax = new Vector2(1, 1);

            if (_fitItemToViewport)
            {
                listItemPrefab.Size = new Vector2(listItemPrefab.Size.x, _viewport.rect.height);
            }

            _itemSize = listItemPrefab.Size.y;

            _content.sizeDelta = new Vector2(0, _itemSize * items + _spacing * (items - 1));

            if (_centerOnItem)
            {
                _scrollRect.inertia = false;
            }


            _itemsVisible = Mathf.CeilToInt(GetViewportSize() / _itemSize);

            int itemsToInstantiate = _itemsVisible;

            if (_itemsVisible == 1)
            {
                itemsToInstantiate = 5;
            }
            else if (itemsToInstantiate < items)
            {
                itemsToInstantiate *= 2;
            }

            if (itemsToInstantiate > items)
            {
                itemsToInstantiate = items;
            }

            _itemsList = new List <ListItemBase> ();

            for (int i = 0; i < itemsToInstantiate; i++)
            {
                ListItemBase item = CreateNewItem(listItemPrefab, i, _itemSize);
                item.onSelected = HandleOnSelectedHandler;
                item.Index      = i;

                _itemsList.Add(item);

                ItemLoaded(item);
            }

            _itemsTotal = items;

            _lastItemIndex = _itemsList.Count - 1;

            _itemsToRecycleAfter = _itemsList.Count - _itemsVisible;


            _scrollRect.onValueChanged.AddListener((Vector2 position) =>
            {
                if (!_centerOnItem)
                {
                    Recycle();
                }
            });
        }