private void SetPostion(TableViewItem item) { var index = item.Index; if (index >= 0) { RectTransform cellRectTrans = item.gameObject.GetComponent <RectTransform>(); switch (arrangement) { case TableView.Arrangement.Vertical: //float pivotX = maxPerLine == 1 ? 0.5f : 1.0f; //多行多列时候向左靠齐 cellRectTrans.anchorMin = new Vector2(0.5f, 1f); cellRectTrans.anchorMax = new Vector2(0.5f, 1f); cellRectTrans.pivot = new Vector2(0.5f, 1f); break; case TableView.Arrangement.Horizontal: cellRectTrans.anchorMin = new Vector2(0f, 0.5f); cellRectTrans.anchorMax = new Vector2(0f, 0.5f); cellRectTrans.pivot = new Vector2(0f, 0.5f); break; } cellRectTrans.localPosition = GetLocalPositionByIndex(index); //cellRectTrans.anchoredPosition3D if (SetItemCall != null) { item.gameObject.name = (index < 10) ? ("0" + index) : ("" + index); SetItemCall(item.gameObject, index); } } }
/// <summary> /// 添加当前数据索引数据 /// </summary> /// <param name="dataIndex">下标,从0开始</param> public void AddItem(int dataIndex) { if (dataIndex < 0 || dataIndex > dataCount) { return; } //检测是否需添加gameObject bool isNeedAdd = false; for (int i = listItem.Count - 1; i >= 0; i--) { TableViewItem item = listItem[i]; if (item.Index >= (dataCount - 1)) { isNeedAdd = true; break; } } SetDataCount(dataCount + 1); if (isNeedAdd) { for (int i = 0; i < listItem.Count; i++) { TableViewItem item = listItem[i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex + 1; item.Updata(_dataProvider[item.Index]); } item = null; } SetUpdateRectItem(GetCurScrollPerLineIndex()); } else { //重新刷新数据 for (int i = 0; i < listItem.Count; i++) { TableViewItem item = listItem[i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex; item.Updata(_dataProvider[item.Index]); } item = null; } } }
/// <summary> /// 重设列表 /// </summary> /// <param name="dataCount">列表长度</param> public void ReLoad(int dataCount) { //移除 for (int i = listItem.Count - 1; i >= 0; i--) { TableViewItem item = listItem[i]; listItem.Remove(item); GameObject.Destroy(item.gameObject); } SetDataCount(dataCount); unUseItem.Clear(); SetUpdateRectItem(0); StartCoroutine(StartContinue()); }
/// <summary> /// 删除当前数据索引下数据 /// </summary> /// <param name="dataIndex">索引下标,从0开始</param> 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--) { TableViewItem item = listItem[i]; int oldIndex = item.Index; if (oldIndex == dataIndex) { listItem.Remove(item); if (isNeedDestroyGameObject) { GameObject.Destroy(item.gameObject); } else { item.Index = -1; item.Updata(null); unUseItem.Enqueue(item); } } if (oldIndex > dataIndex) { item.Index = oldIndex - 1; item.Updata(_dataProvider[item.Index]); } } 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--) { TableViewItem item = listItem[i]; int index = item.Index; if (index < startDataIndex || index >= endDataIndex) { item.Index = -1; item.Updata(null); unUseItem.Enqueue(item); listItem.Remove(item); } } //显示 for (int dataIndex = startDataIndex; dataIndex < endDataIndex; dataIndex++) { if (dataIndex >= dataCount) { continue; } if (IsExistDataByDataIndex(dataIndex)) { continue; } CreateItem(dataIndex); } }
/// <summary> /// 获取所在的下标 /// </summary> /// <param name="itemObj">对象</param> /// <returns></returns> public int IndexOf(GameObject itemObj) { TableViewItem item = itemObj.gameObject.GetComponent <TableViewItem>(); return(listItem.IndexOf(item)); }