mirror of
https://github.com/568071718/creator-collection-view
synced 2025-04-21 08:38:40 +00:00
[feat] 支持scrollToIndex接口
This commit is contained in:
parent
a7834bcf15
commit
ecb0ce3b49
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user