улучшил перерасчет высоты но все еще не идеально

This commit is contained in:
Georgy Shive 2025-03-24 19:46:09 +07:00
parent 7af1d174fc
commit 7fd18874f0

View File

@ -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;
}
/**