HarmonyOS鸿蒙Next中求助!下列代码中List列表组件渲染的时候,onMove为什么没生效

HarmonyOS鸿蒙Next中求助!下列代码中List列表组件渲染的时候,onMove为什么没生效

@Observed
class Article {
  id: string;
  title: string;
  brief: string;
  isLiked: boolean;
  likesCount: number;

  constructor(id: string,
    title: string,
    brief: string,
    isLiked: boolean = false,
    likesCount: number = 0
  ) {
    this.id = id
    this.title = title
    this.brief = brief
    this.isLiked = isLiked
    this.likesCount = likesCount
  }
}



@Component
struct  ArticleCard {
  @ObjectLink article: Article

  handleLiked() {
    this.article.isLiked = !this.article.isLiked;
    this.article.likesCount = this.article.isLiked ? this.article.likesCount+1 : this.article.likesCount - 1
  }

  build() {
    Row(){
      Image($r('app.media.startIcon'))
        .width(80)
        .height(80)
        .margin({right:20})

      Column(){
        Text(this.article.title)
          .fontSize(20)
          .margin({bottom:8})
        Text(this.article.brief)
          .fontSize(16)
          .fontColor(Color.Gray)
          .margin({bottom:8})

        Row(){
          Image(this.article.isLiked ? $r('app.media.liked'): $r('app.media.like'))
            .width(24)
            .height(24)
            .margin({right:8})
          Text(this.article.likesCount.toString())
            .fontSize(20)
        }
        .onClick(()=>{this.handleLiked()})
        .justifyContent(FlexAlign.Center)
      }
      .alignItems(HorizontalAlign.Start)
      .width('80%')
      .height('100%')
    }
    .padding(20)
    .backgroundColor('#FFECECEC')
    .borderRadius(12)
    .height(120)
    .width('100%')
    .justifyContent(FlexAlign.SpaceBetween)
  }
}

@Entry
@Component
struct ArticleListView{

  isListReachEnd: boolean = false

  @State articleList: Array<Article> = [
    new Article('001', '第0篇文章', '文章简介内容', false, 100),
    new Article('002', '第1篇文章', '文章简介内容', false, 100),
    new Article('003', '第2篇文章', '文章简介内容', false, 100),
    new Article('004', '第4篇文章', '文章简介内容', false, 100),
    new Article('005', '第5篇文章', '文章简介内容', false, 100),
    new Article('006', '第6篇文章', '文章简介内容', false, 100),
  ]

  loadMoreArticle() {
    this.articleList.push(new Article('007', '加载的新文章', '文章简介内容'))
  }

  build() {
    Column({space:5}){

      List() {
        ForEach(this.articleList, (item: Article) => {
          ListItem() {
            ArticleCard({article: item})
              .margin({top: 20})
          }
          .draggable(true)
        }, (item: Article) => item.id)
          .onMove((from: number, to: number) => {
            let tmp = this.articleList.splice(from, 1);
            this.articleList.splice(to, 0, tmp[0]);
          })
      }
      .padding(20)
      .scrollBar(BarState.Off)
      .onReachEnd(() => {
        this.isListReachEnd = true
      })
      .parallelGesture(
        PanGesture({direction: PanDirection.Up, distance:80})
          .onActionStart(()=>{
            if (this.isListReachEnd) {
              this.loadMoreArticle()
              this.isListReachEnd = false
            }
          })
      )

    }
    .height('100%')
    .width('100%')
    .backgroundColor(0xF1F3F5)
  }
}

更多关于HarmonyOS鸿蒙Next中求助!下列代码中List列表组件渲染的时候,onMove为什么没生效的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

代码修改完毕,你多些了一行代码,注释掉就行了: .draggable(true)  注释掉这一行就可以了

@Observed
class Article {
  id: string;
  title: string;
  brief: string;
  isLiked: boolean;
  likesCount: number;

  constructor(id: string,
    title: string,
    brief: string,
    isLiked: boolean = false,
    likesCount: number = 0
  ) {
    this.id = id
    this.title = title
    this.brief = brief
    this.isLiked = isLiked
    this.likesCount = likesCount
  }
}


@Component
struct ArticleCard {
  @ObjectLink article: Article

  handleLiked() {
    this.article.isLiked = !this.article.isLiked;
    this.article.likesCount = this.article.isLiked ? this.article.likesCount + 1 : this.article.likesCount - 1
  }

  build() {
    Row() {
      Image($r('app.media.startIcon'))
        .width(80)
        .height(80)
        .margin({ right: 20 })

      Column() {
        Text(this.article.title)
          .fontSize(20)
          .margin({ bottom: 8 })
        Text(this.article.brief)
          .fontSize(16)
          .fontColor(Color.Gray)
          .margin({ bottom: 8 })

        Row() {
          Image(this.article.isLiked ? $r('app.media.abc') : $r('app.media.abc'))
            .width(24)
            .height(24)
            .margin({ right: 8 })
          Text(this.article.likesCount.toString())
            .fontSize(20)
        }
        .onClick(() => {
          this.handleLiked()
        })
        .justifyContent(FlexAlign.Center)
      }
      .alignItems(HorizontalAlign.Start)
      .width('80%')
      .height('100%')
    }
    .padding(20)
    .backgroundColor('#FFECECEC')
    .borderRadius(12)
    .height(120)
    .width('100%')
    .justifyContent(FlexAlign.SpaceBetween)
  }
}

@Entry
@Component
struct ArticleListView {
  isListReachEnd: boolean = false
  @State articleList: Array<Article> = [
    new Article('001', '第0篇文章', '文章简介内容', false, 100),
    new Article('002', '第1篇文章', '文章简介内容', false, 100),
    new Article('003', '第2篇文章', '文章简介内容', false, 100),
    new Article('004', '第4篇文章', '文章简介内容', false, 100),
    new Article('005', '第5篇文章', '文章简介内容', false, 100),
    new Article('006', '第6篇文章', '文章简介内容', false, 100),
  ]

  loadMoreArticle() {
    this.articleList.push(new Article('007', '加载的新文章', '文章简介内容'))
  }

  build() {
    Column({ space: 5 }) {

      List() {
        ForEach(this.articleList, (item: Article) => {
          ListItem() {
            ArticleCard({ article: item })
              .margin({ top: 20 })
          }
          // .draggable(true)    //注释掉这一行就可以了
        }, (item: Article) => item.id)
          .onMove((from: number, to: number) => {
            let tmp = this.articleList.splice(from, 1);
            this.articleList.splice(to, 0, tmp[0]);
          })
      }
      .padding(20)
      .scrollBar(BarState.Off)
      .onReachEnd(() => {
        this.isListReachEnd = true
      })
      .parallelGesture(
        PanGesture({ direction: PanDirection.Up, distance: 80 })
          .onActionStart(() => {
            if (this.isListReachEnd) {
              this.loadMoreArticle()
              this.isListReachEnd = false
            }
          })
      )

    }
    .height('100%')
    .width('100%')
    .backgroundColor(0xF1F3F5)
  }
}

更多关于HarmonyOS鸿蒙Next中求助!下列代码中List列表组件渲染的时候,onMove为什么没生效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


多谢了。 的确不用加那一行(之前我也没加,触控板不好拖拽我以为我代码有问题,问了ai让我加了那一行),

在HarmonyOS Next中,List组件的onMove事件未生效,通常是因为未正确启用列表项拖拽功能。需在List组件中设置editMode为true,并在ListItem组件中设置selected属性以控制拖拽状态。同时,确保onMove回调函数正确绑定并处理索引交换逻辑。检查代码中是否遗漏这些关键配置。

在 HarmonyOS Next 中,List 组件的 onMove 事件需要与 editMode 属性配合使用才能生效。你的代码中缺少了 editMode(true) 的设置。

onMoveList 在编辑模式下,列表项被移动时触发的回调。要使拖拽排序生效,需要:

  1. List 设置 editMode(true) 开启编辑模式。
  2. ListItem 设置 draggable(true) 允许拖拽。

修改你的 List 部分代码:

List({ space: 20, editMode: true }) {
  ForEach(this.articleList, (item: Article) => {
    ListItem() {
      ArticleCard({ article: item })
        .margin({ top: 20 })
    }
    .draggable(true)
  }, (item: Article) => item.id)
  .onMove((from: number, to: number) => {
    let tmp = this.articleList.splice(from, 1);
    this.articleList.splice(to, 0, tmp[0]);
    return true; // 必须返回true确认移动
  })
}

关键修改:

  • List 构造函数中添加 editMode: true 参数
  • onMove 回调中需要返回 true 来确认移动操作

这样配置后,长按列表项即可进入编辑模式进行拖拽排序,onMove 回调会正常触发并更新数据源。

回到顶部