Browse Source

聊天页面,订单卡片

xuqm 1 month ago
parent
commit
ecfef2154b

+ 1 - 0
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
 }

+ 34 - 1
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)