From ecb0ce3b494a6e4a9228a4ef5cd61db80a351003 Mon Sep 17 00:00:00 2001 From: leowu56 <160214186+leowu56@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:10:33 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=94=AF=E6=8C=81scrollToIndex?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list-3x/assets/lib/yx-collection-view.ts | 33 +++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/list-3x/assets/lib/yx-collection-view.ts b/list-3x/assets/lib/yx-collection-view.ts index cef975e..944d186 100644 --- a/list-3x/assets/lib/yx-collection-view.ts +++ b/list-3x/assets/lib/yx-collection-view.ts @@ -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 } -} \ No newline at end of file +}