public void Init(int dataCount) { if (scrollRect == null || content == null || goItemPrefab == null) { TTLoger.LogError("异常:请检测<" + gameObject.name + ">对象上UIWarpContent对应ScrollRect、Content、GoItemPrefab 是否存在值...." + scrollRect + " _" + content + "_" + goItemPrefab); return; } if (dataCount < 0) { return; } setDataCount(dataCount); if (arrangement == Arrangement.Page) { scrollRect.inertia = false; } //移除 for (int i = listItem.Count - 1; i >= 0; i--) { UIWarpContentItem item = listItem[i]; item.Index = -1; unUseItem.Enqueue(item); } listItem.Clear(); setUpdateRectItem(getCurScrollPerLineIndex()); }
/// <summary> /// 添加当前数据索引数据 /// </summary> /// <param name="dataIndex"></param> public void AddItem(int dataIndex) { print("dataIndex" + dataIndex + "datacount" + dataCount); if (dataIndex < 0 || dataIndex > dataCount) { return; } //检测是否需添加gameObject bool isNeedAdd = false; for (int i = listItem.Count - 1; i >= 0; i--) { UIWarpContentItem item = listItem [i]; if (item.Index >= (dataCount - 1)) //已经显示的下标是最后一个数据或倒数第二个数据,就需要添加 { isNeedAdd = true; break; } } setDataCount(dataCount + 1); //重新设置item总量 if (isNeedAdd) { for (int i = 0; i < listItem.Count; i++) { UIWarpContentItem item = listItem [i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) //如果已显示的item索引大于等于添加的索引就增加一个index { item.Index = oldIndex + 1; } item = null; } setUpdateRectItem(getCurScrollPerLineIndex()); //设置显示 } else { //重新刷新数据 for (int i = 0; i < listItem.Count; i++) { UIWarpContentItem item = listItem [i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex; Debug.Log(item.gameObject.name + item.Index); } item = null; } } }
/** * @des:添加当前数据索引数据 */ public void AddItem(int dataIndex) { if (dataIndex < 0 || dataIndex > dataCount) { return; } //检测是否需添加gameObject bool isNeedAdd = false; for (int i = listItem.Count - 1; i >= 0; i--) { UIWarpContentItem item = listItem [i]; if (item.Index >= (dataCount - 1)) { isNeedAdd = true; break; } } setDataCount(dataCount + 1); if (isNeedAdd) { for (int i = 0; i < listItem.Count; i++) { UIWarpContentItem item = listItem [i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex + 1; } item = null; } setUpdateRectItem(getCurScrollPerLineIndex()); } else { //重新刷新数据 for (int i = 0; i < listItem.Count; i++) { UIWarpContentItem item = listItem [i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex; } item = null; } } }
/** * @des:设置更新区域内item * 功能: * 1.隐藏区域之外对象 * 2.更新区域内数据 * 3.Arrangement类型为page时,参数为当前页 */ private void setUpdateRectItem(int scrollPerLineIndex) { if (scrollPerLineIndex < 0) { return; } curScrollPerLineIndex = scrollPerLineIndex; int startDataIndex = curScrollPerLineIndex * maxPerLine; int endDataIndex = (curScrollPerLineIndex + viewCount) * maxPerLine; //分页显示时,显示区域加上后一页的内容 if (arrangement == Arrangement.Page) { curScrollPage = scrollPerLineIndex; startDataIndex = curScrollPage * linePerPage * maxPerLine; endDataIndex = (curScrollPage + 2) * linePerPage * maxPerLine; } //移除 for (int i = listItem.Count - 1; i >= 0; i--) { UIWarpContentItem item = listItem[i]; int index = item.Index; if (index < startDataIndex || index >= endDataIndex || index >= dataCount) { item.Index = -1; listItem.Remove(item); unUseItem.Enqueue(item); } } //显示 for (int dataIndex = startDataIndex; dataIndex < endDataIndex; dataIndex++) { if (dataIndex >= dataCount) { continue; } if (isExistDataByDataIndex(dataIndex)) { continue; } createItem(dataIndex); } }
/** * @des:删除当前数据索引下数据 */ public void DelItem(int dataIndex) { if (dataIndex < 0 || dataIndex >= dataCount) { return; } //删除item逻辑三种情况 //1.只更新数据,不销毁gameObject,也不移除gameobject //2.更新数据,且移除gameObject,不销毁gameObject //3.更新数据,销毁gameObject bool isNeedDestroyGameObject = (listItem.Count >= dataCount); setDataCount(dataCount - 1); for (int i = listItem.Count - 1; i >= 0; i--) { UIWarpContentItem item = listItem [i]; int oldIndex = item.Index; if (oldIndex == dataIndex) { listItem.Remove(item); if (isNeedDestroyGameObject) { GameObject.Destroy(item.gameObject); } else { item.Index = -1; unUseItem.Enqueue(item); } } if (oldIndex > dataIndex) { item.Index = oldIndex - 1; } } setUpdateRectItem(getCurScrollPerLineIndex()); }
/// <summary> /// 设置更新区域内item 功能:1.隐藏区域之外对象 2.更新区域内数据 /// </summary> /// <param name="scrollPerLineIndex"></param> private void setUpdateRectItem(int scrollPerLineIndex) { if (scrollPerLineIndex < 0) { return; } curScrollPerLineIndex = scrollPerLineIndex; int startDataIndex = curScrollPerLineIndex * maxPerLine; //要显示区域的开始数据坐标 int endDataIndex = (curScrollPerLineIndex + viewCount) * maxPerLine; //要显示区域的结束数据坐标 //移除 for (int i = listItem.Count - 1; i >= 0; i--) { UIWarpContentItem item = listItem[i]; int index = item.Index; if (index < startDataIndex || index >= endDataIndex) { item.Index = -1; listItem.Remove(item); //从显示集合移除 unUseItem.Enqueue(item); //入没有使用队列 } } //显示 for (int dataIndex = startDataIndex; dataIndex < endDataIndex; dataIndex++) { if (dataIndex >= dataCount) { continue; } if (isExistDataByDataIndex(dataIndex)) //要显示的数据已经在显示列表中就跳过循环 { continue; } createItem(dataIndex); } }
/** * @des:设置更新区域内item * 功能: * 1.隐藏区域之外对象 * 2.更新区域内数据 */ private void UpdateDataByIndex(int scrollPerLineIndex) { if (scrollPerLineIndex < 0) { return; } curScrollPerLineIndex = scrollPerLineIndex; int startDataIndex = curScrollPerLineIndex * maxPerLine; int endDataIndex = (curScrollPerLineIndex + viewCount) * maxPerLine; //移除 for (int i = listItem.Count - 1; i >= 0; i--) { UIWarpContentItem item = listItem[i]; int index = item.Index; if (index < startDataIndex || index >= endDataIndex) { item.Index = -1; listItem.Remove(item); unUseItem.Enqueue(item); } } //显示 for (int dataIndex = startDataIndex; dataIndex < endDataIndex; dataIndex++) { if (dataIndex >= dataCount) { continue; } if (isExistDataByDataIndex(dataIndex)) { continue; } createItem(dataIndex); } }
/** * @des:创建元素 * @param:dataIndex */ private void createItem(int dataIndex) { UIWarpContentItem item = null; if (unUseItem.Count > 0) { item = unUseItem.Dequeue(); } else { //Debug.Log(goItemPrefab); //Debug.Log(content); if (goItemPrefab != null) { item = addChild(goItemPrefab, content).AddComponent <UIWarpContentItem>(); } } if (item != null) { item.WarpContent = this; item.Index = dataIndex; listItem.Add(item); } }
public void UpdateAll(int dataCount) { curScrollPerLineIndex = -1; switch (arrangement) { case Arrangement.Horizontal: scrollRect.horizontalNormalizedPosition = 0; break; case Arrangement.Vertical: scrollRect.verticalNormalizedPosition = 1; break; } for (int i = listItem.Count - 1; i >= 0; i--) { if (i >= dataCount) { UIWarpContentItem item = listItem[i]; item.Index = -1; listItem.Remove(item); unUseItem.Enqueue(item); } } setDataCount(dataCount); scrollRect.onValueChanged.RemoveAllListeners(); if (enableWrappable) { scrollRect.onValueChanged.AddListener(onValueChanged); } for (int i = listItem.Count - 1; i >= 0; i--) { UIWarpContentItem item = listItem[i]; item.Index = i; } setUpdateRectItem(0); }
//得到当前选择城市 string GetSelectCity() { UIWarpContentItem items = ArrayHelper.Min <UIWarpContentItem, float>(rightChilds.ToArray(), p => Vector3.Distance(p.transform.position, kuang.position)); return(items.GetComponent <Text>().text); }