HarmonyOS 鸿蒙Next Swiper组件刷新问题

HarmonyOS 鸿蒙Next Swiper组件刷新问题

主页面有个swiper组件,用于显示用户已连接的打印机信息(图标、名称、状态等)。 现在需要实时更新打印机的状态,状态更新用ObjectLink。 问题是:有时候可以更新,有时候不能更新。

3 回复

// 数据结构 增加ObservedV2 装饰器

import { ListItemAdapter } from '../common/ListItemAdapter'

import { router } from '[@kit](/user/kit).ArkUI'

[@Observed](/user/Observed)

export class IFlowArticle {

  url: ResourceStr = ''

  title: string = ''

  constructor(url: ResourceStr, title: string) {

    this.url = url

    this.title = title

  }

}

[@Entry](/user/Entry)

[@Component](/user/Component)

export struct SwiperDemoPage {

  [@State](/user/State) private channelItemsDataSource: ListItemAdapter<IFlowArticle> = new ListItemAdapter()

  [@State](/user/State) title: string = ""

  aboutToAppear(): void {

    let ItemsDataSource: IFlowArticle[] = []

    for (let i = 0; i <= 10; i++) {

      ItemsDataSource.push(new IFlowArticle($r('sys.media.ohos_app_icon'), `hello${i}`))

    }

    this.channelItemsDataSource.addList(ItemsDataSource)

  }

  build() {

    Column() {

      Swiper() {

        LazyForEach(this.channelItemsDataSource, (item: IFlowArticle, index: number) => {

          Column(){

            child({ item: item, title: this.title })

          }

          .onClick(() => {

            let item = this.channelItemsDataSource.getData(index)

            if (item) {

              item.title = `等我很久了吧${index}`

              this.channelItemsDataSource?.replaceData(index, item)

            }

            this.channelItemsDataSource.deleteData(this.channelItemsDataSource.totalCount() - 1)

            router.pushUrl({

              url: "pages/Index"

            })

          })

        }, (item: IFlowArticle, index?: number) => "" + index)

      }

      .width('100%')

      .height('100%')

      .indicator(false)

      .loop(false)

      .vertical(false)

      .duration(250)

      .onChange((index: number) => {

        this.title = `item update${index}`

      })

    }

    .width('100%')

    .height('100%')

  }

}

[@Component](/user/Component)

struct child {

  [@ObjectLink](/user/ObjectLink) [@Watch](/user/Watch)("dataChange") item: IFlowArticle

  [@Prop](/user/Prop) title: string

  dataChange(){

    console.log("dataChange")

  }

  build() {

    Column() {

      Image(this.item.url)

        .width(24)

        .height(24)

      Text(this.item.title)

        .fontColor(Color.Red)

        .fontSize(20)

        .margin({ top: 12, bottom: 12 })

        .onClick(()=>{

          this.item.title = "内部组件改变数据监听"

        })

      Text(this.title)

        .fontColor(Color.Green)

        .fontSize(20)

        .margin({ top: 12 })

    }

  }

}

参考链接: https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/arkts-rendering-control-lazyforeach.md#-13

更多关于HarmonyOS 鸿蒙Next Swiper组件刷新问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


具体情况呢?问题图片呢?代码呢?

针对HarmonyOS 鸿蒙Next Swiper组件刷新问题,以下是一些可能的解决方案:

Swiper组件在鸿蒙系统中主要用于提供子组件的滑动轮播显示能力。若遇到刷新问题,首先检查Swiper组件的刷新机制是否正确设置。例如,确保使用了Refresh组件与Swiper组件结合,以实现下拉刷新效果,并正确配置了refreshing状态和onRefreshing事件处理函数。

此外,若Swiper组件在滑动过程中出现卡顿或刷新不流畅的情况,可尝试优化Swiper的子组件加载方式。利用Swiper的预加载机制(cachedCount属性),在主线程空闲时提前构建和布局绘制组件,从而提高滑动时的流畅性和响应速度。

若问题依旧存在,建议检查以下几点:

  • 确保鸿蒙系统版本支持当前使用的Swiper组件API。
  • 检查代码中是否存在内存泄漏或资源未正确释放的情况。
  • 确认Swiper组件的子组件是否都正确加载且没有异常。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部