HarmonyOS鸿蒙Next中List控件某个item局部刷新的文档或者demo

HarmonyOS鸿蒙Next中List控件某个item局部刷新的文档或者demo 请问有List控件某个item局部刷新的文档或者demo么

3 回复

参考简版demo:

@class Task { static id: number = 1 name: string = 任务${Task.id++} finished: boolean = false }

@class StatInfo { totalTask: number = 0 finishTask: number = 0 }

@function card() { .width(‘95%’) .padding(20) .backgroundColor(Color.White) .borderRadius(15) .shadow({ radius: 6, color: ‘#1F000000’, offsetX: 2, offsetY: 4 }) }

@Extend(Text) @function finishTask() { .decoration({ type: TextDecorationType.LineThrough, color: ‘#B1B2B1’ }) .fontColor(’#B1B2B1’) }

@Component struct TaskStatistics { @Consume stat: StatInfo build() { Row() { Text(‘任务:’) .fontSize(30) .fontWeight(FontWeight.Bold) Stack() { Progress({ value: this.stat.finishTask, total: this.stat.totalTask, type: ProgressType.Ring }) .width(100) Row() { Text(this.stat.finishTask.toString()) .fontSize(24) .fontColor(’#36D’) Text(’ / ’ + this.stat.totalTask.toString()) .fontSize(24) } } } .card() .margin({ top: 5, bottom: 10 }) .justifyContent(FlexAlign.SpaceEvenly) } }

@Component struct TaskList { @Consume stat: StatInfo @State tasks: Task[] = [] handleTaskChange: (res: string) => void = (res) => { console.log(res) this.stat.totalTask = this.tasks.length this.stat.finishTask = this.tasks.filter(item => item.finished).length } build() { Column() { Button(‘新增’) .width(200) .margin({ bottom: 10 }) .onClick(() => { this.tasks.push(new Task()) this.handleTaskChange(‘2’) }) List({ space: 10 }) { ForEach(this.tasks, (task: Task, index) => { ListItem() { TaskListItem({ item: task, onTaskChange: this.handleTaskChange }) } .swipeAction({ end: this.DeleteButton(index) }) }) } } }

@Builder DeleteButton(index: number) { Button() { Image(’’) .backgroundColor(Color.Red) .width(20) .aspectRatio(1) } .width(40) .height(40) .type(ButtonType.Circle) .backgroundColor(Color.Red) .margin(5) .onClick(() => { this.tasks.splice(index,1) this.handleTaskChange(‘删除’) }) } }

@Component struct TaskListItem { @ObjectLink item: Task onTaskChange: (e: string) => void = () => { } build() { Row() { if (this.item.finished) { Text(this.item.name) .finishTask() } else { Text(this.item.name) } Checkbox() .shape(CheckBoxShape.ROUNDED_SQUARE) .select(this.item.finished) .onChange((val) => { this.item.finished = val this.onTaskChange(val? ‘选中’:‘未选中’) }) } .card() .justifyContent(FlexAlign.SpaceBetween) } }

@Entry @Component struct TaskListPage { @Provide stat: StatInfo = new StatInfo() build() { Column({ space: 10 }) { Text(‘列表’) .fontSize(20) .fontWeight(FontWeight.Bold) .width(‘100%’) .height(56) .padding({left:20}) TaskStatistics() TaskList() } .width(‘100%’) .height(‘100%’) .backgroundColor(’#F1F2F3’) } }

更多关于HarmonyOS鸿蒙Next中List控件某个item局部刷新的文档或者demo的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,List控件的某个item局部刷新可以通过ListContainernotifyItemChanged(int index)方法实现。该方法会通知系统重新绘制指定索引位置的item,而不影响其他item的显示。具体步骤如下:

  1. 获取ListContainer实例:首先确保你已经在布局文件中定义了ListContainer,并在代码中获取了其实例。

  2. 创建Adapter:创建一个继承自BaseItemProvider的适配器,并重写onBindViewHolder方法来绑定数据。

  3. 局部刷新:在需要刷新特定item时,调用ListContainernotifyItemChanged(int index)方法,传入需要刷新的item的索引。

示例代码如下:

ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
MyAdapter adapter = new MyAdapter(dataList);
listContainer.setItemProvider(adapter);

// 刷新索引为2的item
listContainer.notifyItemChanged(2);

在上述代码中,MyAdapter是你自定义的适配器类,dataList是数据源。调用notifyItemChanged(2)后,索引为2的item会重新调用onBindViewHolder方法进行数据绑定和界面更新。

注意:notifyItemChanged方法只会刷新指定的item,不会影响其他item的状态和显示。这种方法适用于需要局部刷新的场景,可以有效提升列表的性能。

在HarmonyOS(鸿蒙)Next中,要实现List控件某个item的局部刷新,可以使用LazyForEach结合@State@Link状态管理。具体步骤如下:

  1. 定义数据源:使用@State@Link管理列表数据。
  2. LazyForEach渲染:在List中使用LazyForEach动态渲染item。
  3. 更新数据:修改指定item的数据,触发UI自动刷新。

示例代码片段:

@State items: Array<Item> = [...]; // 数据源

build() {
  List() {
    LazyForEach(this.items, (item: Item) => {
      ListItem() {
        Text(item.name)
          .onClick(() => {
            item.name = "Updated"; // 更新指定item
          });
      }
    });
  }
}

通过这种方式,点击某个item时,仅更新该item的UI,实现局部刷新。

回到顶部