HarmonyOS Swiper使用LazyForEach,在数据源更新后,页面闪烁

HarmonyOS
17h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

参考demo:

@Entry
@Component
export struct SwiperDemoPage {
  @State message: string = 'demo 页面';
  private channelItemsDataSource?: ArticleItemsDataSource = new ArticleItemsDataSource([]);
  aboutToAppear(): void {
    let item1 = new IFlowArticle()
    item1.url = 'xxxx'
    let item2 = new IFlowArticle()
    item1.title = 'item1'
    item2.title = 'item2'
    this.channelItemsDataSource?.setData(item1, item2)
    setTimeout(() => {
      // 在获取dataSource中获取需要修改的 item
      let item = this.channelItemsDataSource?.getData(0)
      if(item) {
        item.url = 'xxxxx'
        item.title = 'item1 update'
        this.channelItemsDataSource?.replaceData(0, item)
      }
    }, 1000)
  }
  build() {
    Column() {
      Swiper() {
        LazyForEach(this.channelItemsDataSource, (item: IFlowArticle, index: number) => {
          Stack() {
            Image(item.url)
              .width('100%')
              .aspectRatio(16/9)
            Text(item.title)
              .fontColor(Color.Red)
              .fontSize(20)
          }
          .width('100%')
          .height('100%')
         }, (item: IFlowArticle, index?: number) => "" + index)
      }
      .width('100%')
      .height('100%')
      .indicator(false)
      .loop(false)
      .vertical(true)
      .duration(250)
      .curve(Curve.EaseInOut)
    }
    .width('100%')
    .height('100%')
  }
}
// 数据结构 增加ObservedV2 装饰器
@ObservedV2
export class IFlowArticle {
  @Trace url: string = '';
  @Trace title: string = '';
}
export default class BasicDataSource<T> implements IDataSource {
  private listeners: DataChangeListener[] = [];
  protected mDataSource: T[] = [];
  constructor(dataList: T[]) {
    this.mDataSource = dataList;
  }
  public totalCount(): number {
    return this.mDataSource.length
  }
  public find(predicate: (value: T, index: number, obj: T[]) => boolean): T | undefined {
    return this.mDataSource.find(predicate);
  }
  public findIndex(predicate: (value: T, index: number, obj: T[]) => boolean): number {
    return this.mDataSource.findIndex(predicate);
  }
  public getData(index: number): T | undefined {
    return index >= 0 && index < this.totalCount() ? this.mDataSource[index] : undefined;
  }
  public getHead(): T | undefined {
    return this.getData(0);
  }
  public getTail(): T | undefined {
    return this.getData(this.totalCount() - 1);
  }
  public remove(index: number): T | undefined {
    let ret = this.getData(index);
    if (ret !== undefined) {
      this.mDataSource.splice(index, 1);
      this.notifyDataDelete(index);
    }
    return ret;
  }
  public clean() {
    this.mDataSource = [];
    this.notifyDataReload();
  }
  public setData(...data: T[]): void {
    this.mDataSource = [];
    this.mDataSource.push(...data);
    this.notifyDataReload();
  }
  registerDataChangeListener(listener: DataChangeListener): void {
    if (this.listeners.indexOf(listener) < 0) {
      this.listeners.push(listener)
    }
  }
  unregisterDataChangeListener(listener: DataChangeListener): void {
    const pos = this.listeners.indexOf(listener);
    if (pos >= 0) {
      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 ArticleItemsDataSource extends BasicDataSource<IFlowArticle> {
  constructor(channelItems: IFlowArticle[]) {
    super(channelItems)
  }
  public addData(index: number, data: IFlowArticle): void {
    this.mDataSource.splice(index, 0, data);
    this.notifyDataAdd(index);
  }
  public pushData(data: IFlowArticle): void {
    this.mDataSource.push(data);
    this.notifyDataAdd(this.mDataSource.length - 1);
  }
  public replaceData(index: number, data: IFlowArticle) {
    if (index >= 0 && index < this.totalCount()) {
      this.mDataSource[index] = data
      this.notifyDataChange(index)
    }
  }
}
分享
微博
QQ
微信
回复
14h前
相关问题
lazyforeach替换数据源解决方案
740浏览 • 1回复 待解决
HarmonyOS swiper + LazyForEach使用问题
353浏览 • 1回复 待解决
HarmonyOS picker选择器的数据源问题
0浏览 • 0回复 待解决
Swiper 组件嵌套图片刷新数据闪烁
1273浏览 • 1回复 待解决