HarmonyOS ImageKnife图片库加载问题

几张图片放在可以滑动的页面里,首次安装,加载网络图片,上面的图片能加载出来,底下的图片不显示,但是杀死重进就显示出来了。

HarmonyOS
2024-12-28 07:11:43
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

建议使用lazyforeach来加载长列表内容,可以规避该问题,参考代码如下:

import { ImageKnifeComponent, ImageKnifeOption } from '@app/mpoinImageLoader';
import { MyData } from './MyData';

@Entry
@Component
struct Index {
  private dataSource: MyData = new MyData()
  option1:ImageKnifeOption = {
    loadSrc: 'xxxx',
    placeholderSrc: $r('app.media.app_icon'),
  }

  @State option2:ImageKnifeOption = {
    loadSrc: 'xxxx',
    placeholderSrc: $r('app.media.app_icon'),
  }

  private datas: ImageKnifeOption[] = [this.option1,this.option1,this.option1, this.option1, this.option1,this.option1,this.option1]

  aboutToAppear(): void {
    for (let i = 0; i <= 20; i++) {
      this.dataSource.pushData(this.option1)
    }
  }

  build() {
      LazyForEach(this.dataSource,(item: ImageKnifeOption, index: number) => {
        ListItem(){
          ImageKnifeComponent({imageKnifeOption: item}).width(200).height(200)
            .margin({ top: 10, left: 20, right: 20 })
        }
      })
    }
    .width('100%')
    .height('100%')
  }
}
import { ImageKnifeOption } from '@app/mpoinImageLoader';
export class MyData implements IDataSource {
  private listeners: DataChangeListener[] = [];
  private datas: ImageKnifeOption[] = []
  totalCount(): number {
    return this.datas.length
  }
  getData(index: number): ImageKnifeOption {
    return this.datas[index]
  }
  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();
    })
  }
  // 通知LazyForEach组件需要在index对应索引处添加子组件
  notifyDataAdd(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataAdd(index);
    })
  }
  // 通知LazyForEach组件在index对应索引处数据有变化,需要重建该子组件
  notifyDataChange(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataChange(index);
    })
  }
  // 通知LazyForEach组件需要在index对应索引处删除该子组件
  notifyDataDelete(index: number): void {
    this.listeners.forEach(listener => {
      listener.onDataDelete(index);
    })
  }
  // 通知LazyForEach组件将from索引和to索引处的子组件进行交换
  notifyDataMove(from: number, to: number): void {
    this.listeners.forEach(listener => {
      listener.onDataMove(from, to);
    })
  }
  public addData(index: number, data: ImageKnifeOption): void {
    this.datas.splice(index, 0, data);
    this.notifyDataAdd(index);
  }
  public pushData(data: ImageKnifeOption): void {
    this.datas.push(data);
    this.notifyDataAdd(this.datas.length - 1);
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
分享
微博
QQ
微信
回复
2024-12-28 10:21:54


相关问题
HarmonyOS imageknife图片库加载顺序问题
661浏览 • 1回复 待解决
ImageKnife 图片库相关问题
1761浏览 • 1回复 待解决
HarmonyOS图片加载框架ImageKnife
1149浏览 • 1回复 待解决
HarmonyOS ImageKnife加载网络图片失败
794浏览 • 1回复 待解决
有谁知道如何直接调起图片库
2704浏览 • 1回复 待解决
HarmonyOS Imageknife使用问题
728浏览 • 1回复 待解决
HarmonyOS 图片加载问题
694浏览 • 1回复 待解决
ImageKnife无法加载网络jpeg图
7237浏览 • 1回复 待解决
HarmonyOS 三方ImageKnife接口调用
974浏览 • 1回复 待解决
HarmonyOS Image 加载网络图片问题
1924浏览 • 1回复 待解决
图片加载的三方么?
1786浏览 • 1回复 待解决
HarmonyOS imageknife2.x如何切imageknife3.x
1290浏览 • 1回复 待解决