protected void RenderItem(LoopVerticalItem loopItem, int idx) { var oldRect = loopItem.rect; var oldIdx = loopItem.index; if (idx < dataLength) { var templateId = 0; if (onGetItemTemplateType != null) { templateId = onGetItemTemplateType(this.parameter, idx); } var item = OnCreateOrGetItem(idx, templateId, loopItem, content); // // item.name = string.Format("item {0} {1}", idx, templateId); // Debug.LogFormat("RenderItem oldIdx={0},item={1},loopItem={2},idx={3},templateId={4} ", oldIdx, item, loopItem.item, idx, templateId); var rent = item.GetComponent <RectTransform>(); loopItem.transform = rent; loopItem.item = item; loopItem.index = idx; } var gObj = loopItem?.item.gameObject; if (idx >= dataLength) { // CalcBounds (loopItem, oldIdx, oldRect); //删除时候需要计算bounds if (gObj) { gObj.SetActive(false); } loopItem.index = -1; return; } else if (!gObj.activeSelf) { gObj.SetActive(true); } //keep selected if (m_SelecteStyle != null) { if (m_SelecteStyle.loopItem.index == m_SelectedIndex) { m_SelecteStyle.SelectedStyle(); } else if (m_SelecteStyle.loopItem == loopItem) { m_SelecteStyle.CancelStyle(); } } AddShowList(idx); onItemRender(this.parameter, loopItem.item, loopItem.index); //填充内容 // CalcItemBound(loopItem); // CalcBounds (loopItem, oldIdx, oldRect); }
Component OnCreateOrGetItem(int idx, int templateId, LoopVerticalItem loopItem, RectTransform content) { Component item = loopItem.item; if (item == null || loopItem.templateType != templateId) { //还对象到缓存池 m_Pool.Release(loopItem.templateType, (BindableObject)item); bool isNew = false; item = m_Pool.Get(templateId, out isNew); //创建或者从缓存中获取 loopItem.templateType = templateId; if (isNew) { var sourceRT = templates[templateId].GetComponent <RectTransform>(); var itemTrans = item.transform; itemTrans.SetParent(content); itemTrans.localScale = sourceRT.localScale; itemTrans.localRotation = sourceRT.localRotation; itemTrans.localPosition = sourceRT.localPosition; if (onInstantiated != null) { onInstantiated(this.parameter, item, idx); } } var selecteStyle = item.GetComponent <ILoopSelectStyle>(); if (selecteStyle != null) { selecteStyle.InitSytle(loopItem, this); } } return(item); }
protected void RenderItem(LoopVerticalItem loopItem, int idx) { var oldRect = loopItem.rect; var oldIdx = loopItem.index; if (idx < dataLength) { var templateId = 0; if (onGetItemTemplateType != null) { templateId = onGetItemTemplateType(this.parameter, idx); } var item = OnCreateOrGetItem(idx, templateId, loopItem, content); // item.name = string.Format("item {0} {1}", idx, templateId); var rent = item.GetComponent <RectTransform> (); // if (rent.parent == null) { // rent.SetParent (content); // rent.rotation = Quaternion.Euler (0, 0, 0); // rent.localScale = Vector3.one; // // Vector3 newPos = Vector3.zero; // // newPos.x = halfPadding; // // rent.anchoredPosition = newPos; // } loopItem.transform = rent; loopItem.item = item; loopItem.index = idx; } var gObj = loopItem?.item.gameObject; if (idx >= dataLength) { // CalcBounds (loopItem, oldIdx, oldRect); //删除时候需要计算bounds if (gObj) { gObj.SetActive(false); } return; } else if (!gObj.activeSelf) { gObj.SetActive(true); } AddShowList(idx); onItemRender(this.parameter, loopItem.item, loopItem.index); //填充内容 // CalcItemBound(loopItem); // CalcBounds (loopItem, oldIdx, oldRect); }
///<summary> /// 内容滚动 ///</summary> protected void ScrollLoopVerticalItem() { if (velocity.y == 0 && !isTweening) { return; //没有移动不需要计算 } int min = int.MaxValue, max = int.MinValue; int max1; //= GetBottomItem (); int min1; //= GetTopItem (); LoopVerticalItem itemToRender = null; for (int i = 0; i < m_Pages.Count; i++) { var item = m_Pages[i]; if (item.index == -1) { return; } else { min = Math.Min(item.index, min); } max = Math.Max(item.index, max); } if (velocity.y > 0 || tweenDir.y > 0) { UpdateBounds(); // RenderWaitQueue (); max1 = max; min1 = min; // Debug.LogFormat ("m_ViewBounds={0},m_ViewBounds.yMin{1},m_ViewBounds.yMax{2},(min1).yMax={3},(min1).yMax<m_ViewBounds.yMin:{4}",m_ViewBounds, m_ViewBounds.yMin,m_ViewBounds.yMax,GetLoopVerticalItemAt (min1).yMax,Mathf.Abs(GetLoopVerticalItemAt (min1).yMax) < Mathf.Abs (m_ViewBounds.yMin)); while (m_DataLength > max1 && GetLoopVerticalItemAt(min1).yMax < -m_ViewBounds.yMin) //!m_ViewBounds.Overlaps (m_Pages[min1 % pageSize].rect, true)) // move top do down { max1 = max1 + 1; // Debug.LogFormat ("min1={0},max1:{1} yMax:{2}>m_ViewBounds.yMin{3} ", min1, max1, GetLoopVerticalItemAt (min1).yMax, m_ViewBounds.yMin); if (m_DataLength > max1 && (itemToRender = GetLoopVerticalItemAt(max1)) != null && itemToRender.index != max1) { min1 = min1 + 1; RenderItem(itemToRender, max1); } else { break; } } } else if (velocity.y < 0 || tweenDir.y < 0) { UpdateBounds(); // RenderWaitQueue (); max1 = max; min1 = min; while (min1 >= 0 && GetLoopVerticalItemAt(max1).yMin > -m_ViewBounds.yMin + Mathf.Abs(m_ViewBounds.height)) //!m_ViewBounds.Overlaps (m_Pages[max1 % pageSize].rect, true)) //move bottom item to top { min1 = min1 - 1; // Debug.LogFormat ("min1={0},max1:{1} yMin:{2}>m_ViewBounds.yMax{3} ", min1, max1, GetLoopVerticalItemAt (max1).yMin, m_ViewBounds.yMax); if (min1 >= 0 && (itemToRender = GetLoopVerticalItemAt(min1)) != null && itemToRender.index != min1) { max1 = max1 - 1; RenderItem(itemToRender, min1); } else { break; } } } }
///<summary> /// 计算bound 高度 ///</summary> protected void CalcItemBound(LoopVerticalItem loopItem) { RectTransform rectTran = loopItem.transform; if (rectTran.anchorMin != rectTran.anchorMax) //表示宽度适配 { var offsetMin = rectTran.offsetMin; var offsetMax = rectTran.offsetMax; offsetMin.x = halfPadding; offsetMax.x = -halfPadding; rectTran.offsetMin = offsetMin; rectTran.offsetMax = offsetMax; } // Debug.LogFormat("LayoutRebuilder.ForceRebuildLayoutImmediate= {0},frame={1}", loopItem.item, Time.frameCount); LayoutRebuilder.ForceRebuildLayoutImmediate(rectTran); //立马计算布局 var layoutEle = loopItem.item.GetComponent <LayoutElement>(); Vector2 bound = Vector2.zero; bound.x = halfPadding; if (layoutEle != null) { bound.y = layoutEle.preferredHeight + halfPadding; //高度 } else { bound.y = Mathf.Abs(rectTran.rect.height) + halfPadding; } loopItem.SetHeight(bound.y); // Debug.LogFormat ("End item= {0},layoutEle={1},bound={2},frame={3}", loopItem.item, layoutEle, bound, Time.frameCount); // int index = loopItem.index; // //寻找上一条 // //判断方向 // bool findLast = false; // int idx = index - 1; // Vector2 lastPos = Vector2.zero; // if (idx >= 0) { // var lastItem = GetLoopVerticalItemAt (idx); // if (lastItem.index == idx) { // lastPos.y = lastItem.yMax; // bound.x = lastPos.y + halfPadding; // findLast = true; // } // } // if (!findLast && (idx = index + 1) < m_DataLength) //寻找下一条 // { // var lastItem = GetLoopVerticalItemAt (idx); // if (lastItem.index == idx) { // lastPos.y = lastItem.yMin - bound.y; // bound.x = lastPos.y + halfPadding; // // lastPos.y = lastItem.rect.yMin - (rect.height - this.padding); // } // } // loopItem.bounds = bound; // Vector2 pos = rectTran.anchoredPosition; // pos.y = -bound.x ; // rect.height * .5f ; // pos = pos + m_ContentInitializePosition; //开始位置 // rectTran.anchoredPosition3D = Vector3.zero; // rectTran.anchoredPosition = pos; }