GirlList缓存池

This commit is contained in:
2025-09-15 11:46:55 +08:00
parent 413ac5ccf6
commit bef2efc7e3
3 changed files with 117 additions and 23 deletions
@@ -83,23 +83,7 @@ export class NavigationManager {
* 初始化事件监听
*/
private initEventListeners(): void {
// 监听导航面板切换事件
li_EventManager.I.addInnerEL(
InnerMsgCode.Navigation_PanelSwitch,
this.onNavigationEvent,
this
);
}
/**
* 处理导航事件
*/
private onNavigationEvent(panelType: PanelType): void {
console.log(
"[NavigationManager] Received navigation event for:",
panelType
);
this.switchToPanel(panelType);
//
}
/**
@@ -23,6 +23,10 @@ export class GirlListPanel extends li_BaseView {
cache: GirlListItem[] = [];
// 缓存池相关属性
private itemPool: GirlListItem[] = [];
private maxPoolSize: number = 20;
category: number;
private parentPanelName: string = null;
@@ -49,14 +53,52 @@ export class GirlListPanel extends li_BaseView {
cacheCategory: number = -1;
// 从缓存池获取节点
private getItemFromPool(): GirlListItem {
if (this.itemPool.length > 0) {
return this.itemPool.pop();
} else {
// 缓存池为空,检查是否有隐藏的节点可以复用
for (let i = 0; i < this.content.children.length; i++) {
let childNode = this.content.children[i];
if (!childNode.active && childNode !== this.itemInst.node) {
let item = childNode.getComponent(GirlListItem);
if (item) {
return item;
}
}
}
// 没有可复用的节点,创建新节点
let newNode = instantiate(this.itemInst.node);
let item = newNode.getComponent(GirlListItem);
this.content.addChild(newNode);
return item;
}
}
// 将节点回收到缓存池
private returnItemToPool(item: GirlListItem) {
if (this.itemPool.length < this.maxPoolSize) {
item.reset();
this.itemPool.push(item);
} else {
// 缓存池已满,隐藏节点但不销毁
item.reset();
}
}
async refresh(category: number) {
if (category == this.cacheCategory) return;
this.cacheCategory = category;
// 回收当前显示的节点到缓存池
if (this.cache) {
for (let i = this.cache.length - 1; i >= 0; i--) {
this.cache[i].node.destroy();
let item = this.cache[i];
this.returnItemToPool(item);
}
this.cache = [];
}
// 请求列表
@@ -73,12 +115,15 @@ export class GirlListPanel extends li_BaseView {
// 根据数据,刷新界面
const allId = girlData.getGirlIds(category.toString());
for (let id of allId) {
let newNode = instantiate(this.itemInst.node);
let item = newNode.getComponent(GirlListItem);
let item = this.getItemFromPool();
item.refreshData(category, id, this.node);
newNode.active = true;
item.node.active = true;
this.cache.push(item);
this.content.addChild(newNode);
// 确保节点在 content 中(可能已经在了)
if (item.node.parent !== this.content) {
this.content.addChild(item.node);
}
}
}
}
@@ -94,4 +139,33 @@ export class GirlListPanel extends li_BaseView {
// 直接关闭,不需要动画
this.node.active = false;
}
onDestroy() {
// 清理缓存池,销毁所有缓存的节点
for (let item of this.itemPool) {
if (item && item.node) {
item.node.destroy();
}
}
this.itemPool = [];
// 清理当前显示的节点
for (let item of this.cache) {
if (item && item.node) {
item.node.destroy();
}
}
this.cache = [];
// 清理 content 中所有的 GirlListItem 节点
for (let i = this.content.children.length - 1; i >= 0; i--) {
let child = this.content.children[i];
if (child !== this.itemInst.node) {
let item = child.getComponent(GirlListItem);
if (item) {
child.destroy();
}
}
}
}
}
@@ -174,6 +174,42 @@ export class GirlListItem extends Component {
}
}
reset() {
this.id = -1;
this.category = 0;
this.nameKey = "";
this.tagKey = "";
this.baseNode = null;
// 重置UI状态
this.node.active = false;
this.loading.active = false;
// 清理文本内容
if (this.girlName) this.girlName.string = "";
if (this.tags) this.tags.string = "";
if (this.price) this.price.string = "";
// 重置可见性状态
if (this.price?.node) this.price.node.active = false;
if (this.freeNode) this.freeNode.active = false;
if (this.starParent) this.starParent.active = false;
if (this.chatIcon) this.chatIcon.active = false;
if (this.unreleaseCover) this.unreleaseCover.active = false;
// 清理头像
if (this.avatar) {
this.avatar.spriteFrame = null;
}
// 清理星级显示
if (this.stars) {
for (let star of this.stars) {
star.spriteFrame = null;
}
}
}
onClickDetail() {
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
// 是否已经解锁