/// <summary> ///在执行PlayPannelEffect 时隐藏项执行的操作 可以为空 ///用于特定条件下自定义了Item 操作而需要恢复 /// </summary> /// <param name="_operateItemTrans">操作的项</param> /// <param name="_isOpen"></param> protected virtual void LayoutEffect_EffectOfHideItem(BaseLayoutButton _itemButton, bool _isOpen) { if (_isOpen) //需要考虑是否有必要执行 { //由于创建时有缩放 在这里每次打开时恢复正常大小 避免无法显示 _itemButton.rectTransform.localScale = Vector3.one; } }
}//IF /// <summary> /// 生成面板时创建 ItemButton,后面不再重新创建 ,初始创建时列表项以及 /// 用于滚动的额外列表项已经排列整齐,并且不可见状态 /// </summary> protected void LayoutInitial_CreateItemUIButton() { if (!IsFirstTimeShow) { return; //只有第一次展示才生成 } IsFirstTimeShow = false; if (m_HorizontalLayout) { AllViewItemBtnRefences = new List <BaseLayoutButton>(RowNumber * (ColumnNumber + m_ScrollSpeed)); //创建保存可视区域UI按钮的引用数组 } else { AllViewItemBtnRefences = new List <BaseLayoutButton>(ColumnNumber * (RowNumber + m_ScrollSpeed)); } #region 普通的一次全部创建列表项视图 RectTransform _newItemButton; int _xIndex, _yIndex; int itemBtnTotalCount = 0; //需要创建的ItemButton 数量 if (m_HorizontalLayout) { itemBtnTotalCount = RowNumber * (ColumnNumber + m_ScrollSpeed); } else { itemBtnTotalCount = ColumnNumber * (RowNumber + m_ScrollSpeed); } for (int _index = 0; _index < itemBtnTotalCount; ++_index) { _newItemButton = (Instantiate(m_ItemPrefab, m_ListPanelRectTrans) as GameObject).transform as RectTransform; _newItemButton.gameObject.SetActive(true); _newItemButton.gameObject.name = "Item" + _index; _newItemButton.localScale = Vector3.one; _newItemButton.localRotation = Quaternion.identity; if (m_HorizontalLayout) { //从左向右 从上到下创建 _xIndex = _index / (ColumnNumber + m_ScrollSpeed); //显示的行索引 _yIndex = _index % (ColumnNumber + m_ScrollSpeed); //列索引 } else { //从上到下 从左到右创建 _xIndex = _index % (RowNumber + m_ScrollSpeed); //显示行的索引 _yIndex = _index / (RowNumber + m_ScrollSpeed); //列索引 } _newItemButton.anchoredPosition = new Vector2(_yIndex * (itemRect.x + m_ItemSpace.x) + itemRect.x * 0.5f, -1 * _xIndex * (itemRect.y + m_ItemSpace.y) - itemRect.y * 0.5f) + new Vector2(-1 * m_ListPanelRectTrans.sizeDelta.x / 2f, m_ListPanelRectTrans.sizeDelta.y / 2f); BaseLayoutButton _itemButton = _newItemButton.GetComponent <BaseLayoutButton>(); LayoutInitial_AfterItemUIButtonCreate(_itemButton, _index, _xIndex, _yIndex); _newItemButton.gameObject.SetActive(false); AllViewItemBtnRefences.Add(_itemButton); } //Debug.Log("完成阶段 CreateItemButton " +gameObject.name); #endregion }
/// <summary> /// 填充具体的Button的视图 /// </summary> /// <param name="_operateTrans">操作对象</param> /// <param name="_data">数据参数</param> protected void FillItemButtonData(RectTransform _operateTrans, int _dateIndex, int _relativeIndexList, int _relativeIndexItem) { BaseLayoutButton button = _operateTrans.GetComponent <BaseLayoutButton>(); if (button == null) { return; } button.InitialButtonItem(this, _dateIndex, _relativeIndexList, _relativeIndexItem); }
/// <summary> /// 列表项被创建出来之后执行的操作 一般用于进行初始化设置和保存数据 /// </summary> /// <typeparam name="TButton"></typeparam> /// <param name="_operateButton"></param> /// <param name="_index"></param> /// <param name="_rowIndex"></param> /// <param name="_columnIndex"></param> protected virtual void LayoutInitial_AfterItemUIButtonCreate(BaseLayoutButton _itemButton, int _index, int _rowIndex, int _columnIndex) { if (_itemButton == null) { Debug.LogError("LayoutInitial_AfterItemUIButtonCreate Fail,No BaseLayoutButton ComPonent"); return; } _itemButton.StorePosition(_itemButton.rectTransform.anchoredPosition, _itemButton.rectTransform.TransformPoint(Vector3.zero), _index, _index);//获得要显示时的局部坐标和世界坐标 if (OnLayoutInitial_AfterItemUIButtonCreateAct != null) { OnLayoutInitial_AfterItemUIButtonCreateAct(_itemButton); //执行自定义初始化逻辑 } }
/// <summary> /// 数据层有一个对象改变时候 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collEvent"></param> /// <param name="newData"></param> /// <param name="_dex"></param> /// <param name="_data"></param> /// <param name="sourceData"></param> public virtual void DataModelChangeSignal <T>(CollectionEvent collEvent, T newData, int _dex, T _data, IList <T> sourceData) { int maxVaue = 0; //获得当前能显示的数据最大数量(在这个范围内的数据改变需要刷新,否则只需要更新数据源数据即可) if (m_HorizontalLayout) { maxVaue = (m_InitialItemCount + RowNumber - 1) / RowNumber * RowNumber; } else { maxVaue = (m_InitialItemCount + ColumnNumber - 1) / ColumnNumber * ColumnNumber; } maxVaue = Mathf.Max(maxVaue, ColumnNumber * RowNumber); //最小的显示区域既是初始设置的显示区域 RectTransform item = null; // Debug.Log(_dex+ " maxVaue= " +maxVaue + " " + m_InitialItemCount); switch (collEvent) { case CollectionEvent.AddSignal: #region AddSignal if (m_LayoutState != LayoutState.Idle) { Scroll_ForceStopScroll(); DataModelChangeSignal <T>(collEvent, newData, _dex, _data, sourceData); return; } #region AddSignal if (_dex >= maxVaue) { //Debug.Log("新数据不再显示区域不需要重构" + _dex + " m_InitialItemCount=" + m_InitialItemCount); SavaDataSource(sourceData.Count); return; } //新数据不再显示区域不需要重构 ReBuildView(sourceData.Count, true, true); #endregion #endregion break; case CollectionEvent.DeleteSignal: #region DeleteSignal if (m_LayoutState != LayoutState.Idle) { Scroll_ForceStopScroll(); DataModelChangeSignal <T>(collEvent, newData, _dex, _data, sourceData); return; } if (_dex > maxVaue) { // Debug.Log("新数据不再显示区域不需要重构" + _dex + " m_InitialItemCount=" + m_InitialItemCount); Reset_ClearData(true, true, false); //重构数据源 SavaDataSource(sourceData.Count); return; } //新数据不再显示区域不需要重构 ReBuildView(sourceData.Count, true, true); #endregion break; case CollectionEvent.Update: BaseLayoutButton button = GetItemBaseLayoutButtonComponent(_dex); if (button == null) { Debug.LogError("Update Fail Miss " + _dex); return; } if (button.gameObject.activeSelf) { button.UpdateData(collEvent, newData); return; } #region 旧的处理方式 //#region Horizontal //if (m_HorizontalLayout) //{ // for (int row = 0; row < RowNumber; ++row) // { // for (int _column = 0; _column < ColumnNumber + m_ScrollSpeed; ++_column) // { // item = m_ListPanelRectTrans.Getchild_Ex(row * (ColumnNumber + m_ScrollSpeed) + _column); // BaseLayoutButton button = GetItemBaseLayoutButtonComponent(item); // if (button == null) // { // Debug.LogError("Miss " + _dex); // continue; // } // if (button.m_DataIndex == _dex && item.gameObject.activeSelf) // { // button.UpdateData(collEvent, newData); // // Debug.Log(_column + "更新数据 " + item.gameObject.name); // return; // } // } // } // return; //} //#endregion //#region Vertical //for (int _column = 0; _column < ColumnNumber; ++_column) //{ // for (int row = 0; row < RowNumber; ++row) // { // item = m_ListPanelRectTrans.Getchild_Ex(_column * (RowNumber + m_ScrollSpeed) + row); // BaseLayoutButton button = GetItemBaseLayoutButtonComponent(item); // if (button == null) // { // Debug.LogError("Miss " + _dex); // continue; // } // if (button.m_DataIndex == _dex && item.gameObject.activeSelf) // { // // Debug.Log(_column + "更新数据 " + item.gameObject.name); // button.UpdateData(collEvent, newData); // return; // } // } //} //#endregion #endregion break; } }