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); // } if (updateFunc != null) { updateFunc(item.gameObject, index); } } }
/// <summary> /// 更新数据 /// </summary> public void ReloadData(int dataIndex) { if (listItem.Count > dataIndex) { TableViewItem item = listItem [dataIndex]; if (null != updateFunc) { updateFunc(item.gameObject, item.Index); } } }
/// <summary> /// 更新数据 /// </summary> public void ReloadData() { for (int i = 0; i < listItem.Count; i++) { TableViewItem item = listItem [i]; if (null != updateFunc) { updateFunc(item.gameObject, item.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 = null; } if (null != co) { StopCoroutine(co); } co = StartCoroutine(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 = null; } } }
/// <summary> /// 设置更新区域内item, 1.隐藏区域之外对象, 2.更新区域内数据 /// </summary> /// <param name="scrollPerLineIndex">当前滑动到的下标</param> public IEnumerator SetUpdateRectItem(int scrollPerLineIndex) { if (scrollPerLineIndex < 0) { yield return(null); } curScrollPerLineIndex = scrollPerLineIndex; int startDataIndex = curScrollPerLineIndex * maxPerLine; int endDataIndex = (curScrollPerLineIndex + viewCount) * maxPerLine; //Log.Instance.infoFormat ("update rectitem, star:{0}, end:{1}, scrollIndex:{2}", startDataIndex, endDataIndex, scrollPerLineIndex); // 移除 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; unUseItem.Enqueue(item); listItem.Remove(item); // if (item.gameObject.activeSelf) { // item.gameObject.SetActive (false); // } //Log.Instance.infoFormat ("remove item, index:{0}", index); } } // 显示 for (int dataIndex = startDataIndex; dataIndex < endDataIndex; dataIndex++) { if (dataIndex >= dataCount) { continue; } if (IsExistDataByDataIndex(dataIndex)) { continue; } CreateItem(dataIndex); yield return(null); //Log.Instance.infoFormat ("create item, index:{0}", dataIndex); } }
/// <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; unUseItem.Enqueue(item); } } if (oldIndex > dataIndex) { item.Index = oldIndex - 1; } } if (null != co) { StopCoroutine(co); } co = StartCoroutine(SetUpdateRectItem(GetCurScrollPerLineIndex())); }
/// <summary> /// 重设列表 /// </summary> /// <param name="dataCount">列表长度</param> public void ReLoad(int dataCount) { // Log.Instance.infoFormat ("reload data {0}, listSize:{1}", dataCount, listItem.Count); // 移除监听 scrollRect.onValueChanged.RemoveAllListeners(); // 参数检查 CheckArgeIsNull(); //移除 for (int i = listItem.Count - 1; i >= 0; i--) { TableViewItem item = listItem[i]; GameObject.Destroy(item.gameObject); } while (unUseItem.Count > 0) { TableViewItem item = unUseItem.Dequeue(); GameObject.Destroy(item.gameObject); } SetDataCount(dataCount); unUseItem.Clear(); listItem.Clear(); scrollRect.normalizedPosition = Vector2.one; if (null != co) { StopCoroutine(co); } co = StartCoroutine(SetUpdateRectItem(0)); // 避免切换时候滚动现象出现 SetUpdateContentSize(); scrollRect.onValueChanged.AddListener(OnValueChanged); // StartCoroutine(StartContinue()); //SetUpdateContentSize(); }
/// <summary> /// 获取所在的下标 /// </summary> /// <param name="itemObj">对象</param> /// <returns></returns> public int IndexOf(GameObject itemObj) { TableViewItem item = itemObj.gameObject.GetComponent <TableViewItem>(); return(listItem.IndexOf(item)); }