From 7fd18874f06912674635e5e207810d5a2f57ac9f Mon Sep 17 00:00:00 2001 From: Georgy Shive Date: Mon, 24 Mar 2025 19:46:09 +0700 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=BB=D1=83=D1=87=D1=88=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82=20?= =?UTF-8?q?=D0=B2=D1=8B=D1=81=D0=BE=D1=82=D1=8B=20=D0=BD=D0=BE=20=D0=B2?= =?UTF-8?q?=D1=81=D0=B5=20=D0=B5=D1=89=D0=B5=20=D0=BD=D0=B5=20=D0=B8=D0=B4?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Scripts/ui/ErrorLazyLoadList.ts | 120 +++++++++++++------------ 1 file changed, 62 insertions(+), 58 deletions(-) diff --git a/assets/Scripts/ui/ErrorLazyLoadList.ts b/assets/Scripts/ui/ErrorLazyLoadList.ts index 7977d6e..e41dbb0 100644 --- a/assets/Scripts/ui/ErrorLazyLoadList.ts +++ b/assets/Scripts/ui/ErrorLazyLoadList.ts @@ -119,70 +119,74 @@ export class ErrorLazyLoadList extends Component { } } - loadNextBatch(batchSize: number) { - let currentData: any[] = []; - if (this._mode === "errors") { - currentData = this._filteredData || this._data; - } else if (this._mode === "details") { - currentData = this._sensorDescription ? this._sensorDescription.fields : []; - } - if (this._loadedCount >= currentData.length) return; - - // Вычисляем суммарную высоту уже отрисованных элементов - let cumulativeHeight = 0; - for (let i = 0; i < this._content.children.length; i++) { - const child = this._content.children[i]; - const childHeight = child.getComponent(UITransform)?.height || 0; - cumulativeHeight += childHeight + this.spacing; - } - - // Загружаем следующую порцию элементов - const endIndex = Math.min(this._loadedCount + batchSize, currentData.length); - for (let i = this._loadedCount; i < endIndex; i++) { - let item: Node; + loadNextBatch(batchSize: number) { + let currentData: any[] = []; if (this._mode === "errors") { - if (this._filteredData) { - item = instantiate(this.errorItemNamedPrefab); - const errorItemNamedButton = item.getComponent(ErrorItemNamedButton); - if (errorItemNamedButton) { - errorItemNamedButton.initialize(currentData[i]); - } else { - console.warn("Компонент ErrorItemNamedButton не найден на префабе."); - } - } else { - item = instantiate(this.errorItemPrefab); - const errorItemButton = item.getComponent(ErrorItemButton); - if (errorItemButton) { - errorItemButton.initialize(currentData[i]); - } else { - console.warn("Компонент ErrorItemButton не найден на префабе."); - } - } + currentData = this._filteredData || this._data; } else if (this._mode === "details") { - item = instantiate(this.sensorFieldItemPrefab); - const sensorFieldItem = item.getComponent(SensorFieldItem); - if (sensorFieldItem) { - sensorFieldItem.setData(currentData[i]); - } else { - console.warn("Компонент SensorFieldItem не найден на префабе."); - } - } else { - console.error("Неизвестный режим: " + this._mode); - continue; + currentData = this._sensorDescription ? this._sensorDescription.fields : []; } + if (this._loadedCount >= currentData.length) return; + + const endIndex = Math.min(this._loadedCount + batchSize, currentData.length); + for (let i = this._loadedCount; i < endIndex; i++) { + let item: Node; + if (this._mode === "errors") { + if (this._filteredData) { + item = instantiate(this.errorItemNamedPrefab); + const errorItemNamedButton = item.getComponent("ErrorItemNamedButton") as unknown as ErrorItemNamedButton; + if (errorItemNamedButton) { + errorItemNamedButton.initialize(currentData[i]); + } else { + console.warn("Компонент ErrorItemNamedButton не найден на префабе."); + } + } else { + item = instantiate(this.errorItemPrefab); + const errorItemButton = item.getComponent("ErrorItemButton") as unknown as ErrorItemButton; + if (errorItemButton) { + errorItemButton.initialize(currentData[i]); + } else { + console.warn("Компонент ErrorItemButton не найден на префабе."); + } + } + } else if (this._mode === "details") { + item = instantiate(this.sensorFieldItemPrefab); + const sensorFieldItem = item.getComponent(SensorFieldItem); + if (sensorFieldItem) { + sensorFieldItem.setData(currentData[i]); + } else { + console.warn("Компонент SensorFieldItem не найден на префабе."); + } + } else { + console.error("Неизвестный режим: " + this._mode); + continue; + } + // Пока просто добавляем элемент, без позиционирования + this._content.addChild(item); + } + this._loadedCount = endIndex; - // Получаем высоту элемента после установки данных - const itemHeight = item.getComponent(UITransform)?.height || this._itemHeight; - // Устанавливаем позицию: по Y = -(cumulativeHeight + itemHeight/2) - item.setPosition(0, - (cumulativeHeight + itemHeight / 2), 0); - cumulativeHeight += itemHeight + this.spacing; - this._content.addChild(item); + // После добавления новой партии элементов, переспозиционируем всех дочерних + this.scheduleOnce(() => { + this.repositionChildren(); + }, 0); } - this._loadedCount = endIndex; - // Обновляем высоту контейнера - this._contentUI.height = cumulativeHeight; -} + private repositionChildren() { + let cumulativeHeight = 0; + // Проходим по всем дочерним элементам контейнера + for (let i = 0; i < this._content.children.length; i++) { + const child = this._content.children[i]; + // Получаем актуальную высоту через UITransform + const childHeight = child.getComponent(UITransform)?.height || 0; + // Вычисляем позицию для этого элемента так, чтобы его центр был ниже предыдущего элемента + child.setPosition(0, - (cumulativeHeight + childHeight / 2), 0); + // Добавляем к накопленной высоте высоту элемента плюс spacing + cumulativeHeight += childHeight + this.spacing; + } + // Обновляем высоту контейнера, чтобы ScrollView корректно показывал содержимое + this._contentUI.height = cumulativeHeight; + } /**