From ecfef2154bc95a1df1ac8b993234763de44da4be Mon Sep 17 00:00:00 2001 From: xuqm Date: Mon, 17 Mar 2025 16:47:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E9=A1=B5=E9=9D=A2=EF=BC=8C?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=8D=A1=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ets/view/refresh/RefreshController.ets | 1 + src/main/ets/view/refresh/RefreshView.ets | 35 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/ets/view/refresh/RefreshController.ets b/src/main/ets/view/refresh/RefreshController.ets index 9004980..9d806ae 100644 --- a/src/main/ets/view/refresh/RefreshController.ets +++ b/src/main/ets/view/refresh/RefreshController.ets @@ -1,5 +1,6 @@ export interface RefreshController { toTop: (smooth?: boolean) => void toBottom: (smooth?: boolean) => void + toIndex: (value: number, smooth?: boolean, align?: ScrollAlign) => void isAtEnd: () => boolean } \ No newline at end of file diff --git a/src/main/ets/view/refresh/RefreshView.ets b/src/main/ets/view/refresh/RefreshView.ets index 01d21fd..aa39e76 100644 --- a/src/main/ets/view/refresh/RefreshView.ets +++ b/src/main/ets/view/refresh/RefreshView.ets @@ -35,12 +35,16 @@ export struct RefreshView { } else if (this._toBottom) { this.toBottom() this._toBottom = false + } else if (this._toIndex) { + this.toIndex() + this._toIndex = false } } }; private _firstFinish = false private _isLoad = false private _toTop = false + private _toIndex = false private _toBottom = false change() { @@ -58,11 +62,31 @@ export struct RefreshView { this.controller.toBottom = (smooth?: boolean) => { this.toBottom(smooth) } + this.controller.toIndex = (value: number, smooth?: boolean, align?: ScrollAlign) => { + this.toIndex(value, smooth, align) + } this.controller.isAtEnd = () => this._scroller.isAtEnd() } this._listener.on('draw', this.onDrawComplete) } + @State value: number = -1 + @State smooth?: boolean = false + @State aligns?: ScrollAlign = undefined + + toIndex(value?: number, smooth?: boolean, align?: ScrollAlign) { + if (!this._isLoad) { + if (value || this.value != -1 || value === 0) { + this._scroller.scrollToIndex(value ?? this.value, smooth, align) + } + } else { + this.value = value ?? this.value + this.smooth = smooth + this.aligns = align + this._toIndex = true + } + } + toTop(smooth?: boolean) { if (!this._isLoad) { this._scroller.scrollToIndex(0, smooth, ScrollAlign.START) @@ -73,7 +97,7 @@ export struct RefreshView { toBottom(smooth?: boolean) { if (!this._isLoad) { - this._scroller.scrollToIndex(this.data.length - 1, smooth, ScrollAlign.END) + this._scroller.scrollToIndex(this.data.length, smooth, ScrollAlign.END) } else { this._toBottom = true } @@ -83,8 +107,13 @@ export struct RefreshView { this._listener.off('draw', this.onDrawComplete); } + @Builder + doNothingBuilder() { + }; + // 使用父组件@Builder装饰的方法初始化子组件@BuilderParam @BuilderParam customBuilderParam: (item: ESObject, index: number) => void + @BuilderParam headBuilderParam: () => void = this.doNothingBuilder; build() { @@ -93,6 +122,10 @@ export struct RefreshView { List({ scroller: this._scroller, }) { + ListItem() { + this.headBuilderParam() + } + ForEach(this.data ?? [], (item: ESObject, index: number) => { ListItem() { this.customBuilderParam(item, index)