HarmonyOS 如何做到在往列表前面插入数据的时候,列表位置保持不动

如何做到在往列表前面插入数据的时候,列表位置保持不动,保持锚点位置。聊天界面中用户滑动加载历史消息,但是拉回来消息肯定是插到前面,支持用户继续滑动。但是不改变用户当前所在位置。

HarmonyOS
2024-10-16 10:09:44
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

可参考此demo:

// Basic implementation of IDataSource to handle data listener  
abstract class BasicDataSource<T> implements IDataSource {  
  private listeners: DataChangeListener[] = []  
  abstract totalCount(): number  
  abstract getData(index: number): T  
  registerDataChangeListener(listener: DataChangeListener): void {  
    if (this.listeners.indexOf(listener) < 0) {  
      console.info('add listener')  
      this.listeners.push(listener)  
    }  
  }  
  unregisterDataChangeListener(listener: DataChangeListener): void {  
    const pos = this.listeners.indexOf(listener);  
    if (pos >= 0) {  
      console.info('remove listener')  
      this.listeners.splice(pos, 1)  
    }  
  }  
  notifyDataReload(): void {  
    this.listeners.forEach(listener => {  
      listener.onDataReloaded()  
    })  
  }  
  notifyDataAdd(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataAdd(index)  
    })  
  }  
  notifyDataChange(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataChange(index)  
    })  
  }  
  notifyDataDelete(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataDelete(index)  
    })  
  }  
  notifyDataMove(from: number, to: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataMove(from, to)  
    })  
  }  
}  
export class MyDataSource<T> extends BasicDataSource<T> {  
  private dataArray: T[] = []  
  
  public totalCount(): number {  
    return this.dataArray.length  
  }  
  public getData(index: number): T {  
    console.log("zxy List cacheCountTest3 getData = " + index)  
    return this.dataArray[index]  
  }  
  public addData(index: number, data: T): void {  
    this.dataArray.splice(index, 0, data) // 在index位置增加data  
    this.notifyDataAdd(index)  
  }  
  public dataReload(): void {  
    this.notifyDataReload()  
  }  
  public pushData(data: T): void {  
    this.dataArray.push(data)  
    this.notifyDataAdd(this.dataArray.length - 1)  
  }  
  public deleteIndex(index: number): void {  
    this.dataArray.splice(index, 1) // 在index位置删除1个数据  
    this.notifyDataDelete(index)  
  }  
  public deleteData(data: T): void {  
    let index = this.dataArray.indexOf(data)  
    this.dataArray.splice(index, 1) // 在index位置删除1个数据  
    this.notifyDataDelete(index)  
  }  
  public moveData(from: number, to: number) {  
    this.notifyDataMove(from, to)  
  }  
  public changeData(index: number, data: T) {  
    this.dataArray[index] = data  
    this.notifyDataChange(index)  
  }  
  public indexOf(data: T): number {  
    return this.dataArray.indexOf(data)  
  }  
}  
@Entry  
@Component  
struct page240524163225079 {  
  startIndex: number = 10  
  loading: boolean = false;  
  private arr: MyDataSource<number> = new MyDataSource()  
  private scroller: Scroller = new Scroller()  
  
  aboutToAppear(): void {  
    for (let i = 0; i < 16; i++) {  
      this.arr.pushData(i)  
    }  
  }  
  build() {  
    Column() {  
      List({ initialIndex: 10, space: 20, scroller: this.scroller }) {  
        LazyForEach(this.arr, (item: number) => {  
          ListItem() {  
            Column() {  
              Text('' + item)  
                .width('100%')  
                .height(100)  
                .fontSize(16)  
                .textAlign(TextAlign.Center)  
                .borderRadius(10)  
                .backgroundColor(0xFFFFFF)  
              Image($r('app.media.background'))  
                .height(200)  
                .height(200)  
            }  
          }  
        }, (item: string) => item)  
      }.width('90%')  
      .scrollBar(BarState.Off)  
      .onScrollIndex((start: number) => {  
        this.startIndex = start;  
        console.log('this.startIndex', this.startIndex)  
        if (start < 5 && !this.loading) {  
          setTimeout(() => {  
            let indexOffset = this.scroller.getItemRect(this.startIndex).y;  
            let a = this.arr.getData(0)  
            for (let i = 0; i < 10; i++) {  
              this.arr.addData(0, a - i - 1);  
            }  
            this.scroller.scrollToIndex(this.startIndex + 10);  
            this.scroller.scrollBy(0, -indexOffset);  
            this.loading = false;  
          }, 1000)  
          this.loading = true;  
        }  
      })  
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })  
  }  
}
分享
微博
QQ
微信
回复
2024-10-16 15:56:25
相关问题
HarmonyOS 如何做到页面弹框之上?
440浏览 • 1回复 待解决
如何做到点击通知消息打开应用
534浏览 • 1回复 待解决
数组列表如何实现数据双向同步?
362浏览 • 1回复 待解决
如何更新页面列表数据
6971浏览 • 1回复 待解决
鸿蒙一套代码如何做到多端适配呢?
3472浏览 • 1回复 待解决
如何做到类似web中整个app变灰
383浏览 • 1回复 待解决
分组列表实践(嵌套列表
1066浏览 • 1回复 待解决
HarmonyOS 如何获取对象方法列表
425浏览 • 1回复 待解决