/// 创建Element protected UIListViewItem CreateElement(int index, ref stRect rect) { UIListViewItem elementScript = null; //找 if (m_unUsedElementScripts.Count > 0) { elementScript = m_unUsedElementScripts[0]; m_unUsedElementScripts.RemoveAt(0); } else if (m_elementTemplate != null) { //克隆 GameObject elementObject = Instantiate(m_elementTemplate); elementObject.transform.SetParent(m_content.transform); elementObject.transform.localScale = Vector3.one; //初始化 InitializeComponent(elementObject); // elementScript = elementObject.GetComponent <UIListViewItem>(); } if (elementScript != null) { elementScript.Enable(this, index, m_elementName, ref rect, IsSelectedIndex(index)); m_elementScripts.Add(elementScript); //JW.Common.Log.LogD("Dispatch Element On Create Event, index = " + index); if (OnEnableItemHandler != null) { OnEnableItemHandler(elementScript); } } return(elementScript); }
/// 回收Element protected void RecycleElement(UIListViewItem elementScript, bool disableElement) { if (disableElement) { elementScript.Disable(); } m_elementScripts.Remove(elementScript); m_unUsedElementScripts.Add(elementScript); }
/// 回收Element protected void RecycleElement(bool disableElement) { //将当前所有elements设置为unUsed while (m_elementScripts.Count > 0) { UIListViewItem elementScript = m_elementScripts[0]; m_elementScripts.RemoveAt(0); if (disableElement) { elementScript.Disable(); } m_unUsedElementScripts.Add(elementScript); } }
/// 设置选中元素 /// @index /// @isDispatchSelectedChangeEvent public virtual void SelectElement(int index, bool isDispatchSelectedChangeEvent = true) { m_lastSelectedElementIndex = m_selectedElementIndex; m_selectedElementIndex = index; if (m_lastSelectedElementIndex == m_selectedElementIndex) { if (m_alwaysDispatchSelectedChangeEvent) { //派发事件 DispatchElementSelectChangedEvent(); } return; } if (m_lastSelectedElementIndex >= 0) { UIListViewItem elementScript = GetElemenet(m_lastSelectedElementIndex); if (elementScript != null) { elementScript.ChangeDisplay(false); } } if (m_selectedElementIndex >= 0) { UIListViewItem elementScript = GetElemenet(m_selectedElementIndex); if (elementScript != null) { elementScript.ChangeDisplay(true); } } //派发事件 if (isDispatchSelectedChangeEvent) { DispatchElementSelectChangedEvent(); } }
// 初始化 public override void Initialize(UIForm formScript) { if (_isInited) { return; } base.Initialize(formScript); m_selectedElementIndex = -1; m_lastSelectedElementIndex = -1; //初始化Scroll Content m_scrollRect = GetComponentInChildren <ScrollRect>(gameObject); if (m_scrollRect != null) { m_scrollRect.enabled = false; RectTransform scrollRectRectTransform = m_scrollRect.transform as RectTransform; m_scrollAreaSize = new Vector2(scrollRectRectTransform.rect.width, scrollRectRectTransform.rect.height); m_content = m_scrollRect.content.gameObject; } //初始化ScrollBar m_scrollBar = GetComponentInChildren <Scrollbar>(gameObject); if (m_listType == UIListViewType.Vertical || m_listType == UIListViewType.VerticalGrid) { if (m_scrollBar != null) { m_scrollBar.direction = Scrollbar.Direction.BottomToTop; } JW.Common.Log.Assert(m_scrollRect != null); if (m_scrollRect != null) { m_scrollRect.horizontal = false; m_scrollRect.vertical = true; m_scrollRect.horizontalScrollbar = null; m_scrollRect.verticalScrollbar = m_scrollBar; } } else if (m_listType == UIListViewType.Horizontal || m_listType == UIListViewType.HorizontalGrid) { if (m_scrollBar != null) { m_scrollBar.direction = Scrollbar.Direction.LeftToRight; } JW.Common.Log.Assert(m_scrollRect != null); if (m_scrollRect != null) { m_scrollRect.horizontal = true; m_scrollRect.vertical = false; m_scrollRect.horizontalScrollbar = m_scrollBar; m_scrollRect.verticalScrollbar = null; } } //初始化元素列表 m_elementScripts = new JW.Common.JWObjList <UIListViewItem>(); m_unUsedElementScripts = new JW.Common.JWObjList <UIListViewItem>(); if (m_useOptimized) { m_elementsRect = new List <stRect>(); } //模板 UIListViewItem listElementScript = null; listElementScript = GetComponentInChildren <UIListViewItem>(gameObject); if (listElementScript != null) { listElementScript.Initialize(formScript); m_elementTemplate = listElementScript.gameObject; m_elementName = listElementScript.gameObject.name; m_elementDefaultSize = listElementScript.m_defaultSize; // if (m_elementTemplate != null) { m_elementTemplate.name = m_elementName + "_Template"; } } //初始化为不可见 if (m_elementTemplate != null) { UIListViewItem elementScript = m_elementTemplate.GetComponent <UIListViewItem>(); if (elementScript != null && elementScript.m_useSetActiveForDisplay) { m_elementTemplate.ExtSetActive(false); } else { //确保Element被激活 if (!m_elementTemplate.activeSelf) { m_elementTemplate.SetActive(true); } //为List元素模版添加Canvas Group CanvasGroup canvasGroup = m_elementTemplate.GetComponent <CanvasGroup>(); if (canvasGroup == null) { canvasGroup = m_elementTemplate.AddComponent <CanvasGroup>(); } canvasGroup.alpha = 0f; canvasGroup.blocksRaycasts = false; } } //初始化content位置 if (m_content != null) { m_contentRectTransform = m_content.transform as RectTransform; m_contentRectTransform.pivot = new Vector2(0, 1); m_contentRectTransform.anchorMin = new Vector2(0, 1); m_contentRectTransform.anchorMax = new Vector2(0, 1); m_contentRectTransform.anchoredPosition = Vector2.zero; m_contentRectTransform.localRotation = Quaternion.identity; m_contentRectTransform.localScale = new Vector3(1, 1, 1); m_lastContentPosition = m_contentRectTransform.anchoredPosition; } //初始化extraContent if exist if (m_extraContent != null) { RectTransform extraContentTransform = m_extraContent.transform as RectTransform; extraContentTransform.pivot = new Vector2(0, 1); extraContentTransform.anchorMin = new Vector2(0, 1); extraContentTransform.anchorMax = new Vector2(0, 1); extraContentTransform.anchoredPosition = Vector2.zero; extraContentTransform.localRotation = Quaternion.identity; extraContentTransform.localScale = Vector3.one; //宽度与listElement保持一致 if (m_elementTemplate != null) { extraContentTransform.sizeDelta = new Vector2((m_elementTemplate.transform as RectTransform).rect.width, extraContentTransform.sizeDelta.y); } if (extraContentTransform.parent != null && m_content != null) { extraContentTransform.parent.SetParent(m_content.transform); } m_extraContent.SetActive(false); } SetElementAmount(m_elementAmount); }