[feat] 支持scrollToIndex接口

This commit is contained in:
leowu56 2025-02-28 00:10:33 +08:00 committed by GitHub
parent a7834bcf15
commit ecb0ce3b49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1237,6 +1237,37 @@ export class YXCollectionView extends Component {
}
}
/**
*
* @param index (0)
* @param timeInSecond ()
* @param attenuated
*/
scrollToIndex(index: number, timeInSecond: number = 0, attenuated: boolean = true) {
if (index < 0) return;
let currentIndex = 0;
let targetIndexPath: YXIndexPath = null;
// 遍历所有section找到对应索引的位置
for (let section = 0; section < this.getNumberOfSections(); section++) {
const itemsInSection = this.getNumberOfItems(section);
if (currentIndex + itemsInSection > index) {
// 找到目标索引所在的section和item
const item = index - currentIndex;
targetIndexPath = new YXIndexPath(section, item);
break;
}
currentIndex += itemsInSection;
}
if (targetIndexPath) {
this.scrollTo(targetIndexPath, timeInSecond, attenuated);
}
}
/**
*
*/
@ -1517,4 +1548,4 @@ export abstract class YXBinaryLayout extends YXLayout {
return result
}
}
}