HarmonyOS 新闻频道编辑页面,长按手势和拖动手势冲突,无法实现长按拖动功能

新闻频道页面使用Grid组件搭建页面,需要实现长按拖动功能,在给GridItem添加LongPressGesture手势后,仍然无法实现长按拖动,尝试在GridItem子组件中添加组合手势也无效,请问如何处理?期望:新闻频道编辑页面,长按频道可编辑,拖动调整频道顺序。

HarmonyOS
2024-08-04 14:57:23
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
dickhome

解决方案的代码如下:

import display from '@ohos.display'; 
@Entry 
@Component 
struct Index { 
  //是否编辑 
  @State canEdit: boolean = false; 
  @State private visibleChannels: string[] = [ 
    '热点', '国内', '国外', '健康', 
    '娱乐', '社会', '军事', '汽车', 
    '摩托车', '自行车', '航海', '船舶', 
    '深圳', '北京', '上海', '天津', 
    '河北', '石家庄', 
  ]; 
  @State private displayWidth: number = 0; 
  @State private itemWidth: number = 0; 
 
  aboutToAppear(): void { 
    let displayClass = display.getDefaultDisplaySync(); 
    this.displayWidth = px2vp(displayClass.width); 
    this.itemWidth = (this.displayWidth - 83) / 4; 
  } 
  build() { 
    Column() { 
      this.visibleChannelHeader() 
      this.visibleChannelBody() 
    } 
    .backgroundColor(Color.White) 
    .align(Alignment.TopStart) 
  } 
  @Builder 
  visibleChannelHeader() { 
    Row() { 
      Text('我的频道') 
        .fontSize(17) 
        .fontWeight(FontWeight.Bold) 
        .fontColor(0xFF383838) 
        .letterSpacing(1.0) 
      Blank() 
      Text(this.canEdit ? '完成' : '编辑') 
        .fontColor(this.canEdit ? 0xFFB71C17 : 0xFF9E9E9E) 
        .fontSize(14) 
        .onClick(() => { 
          this.canEdit = !this.canEdit 
          console.info("this.canEdit:"+this.canEdit) 
        }) 
    } 
    .width('100%') 
    .padding({ top: 15, left: 14, bottom: 24, right: 14 }) 
  } 
  //交换网格中被拖拽交互的两个元素的位置 
  changeIndex(index1: number, index2: number) { 
    let tmpChannel: string; 
    tmpChannel = this.visibleChannels[index1]; 
    this.visibleChannels[index1] = this.visibleChannels[index2]; 
    this.visibleChannels[index2] = tmpChannel; 
  } 
  @Builder 
  visibleChannelBody() { 
    Grid() { 
      ForEach(this.visibleChannels, (channel: string) => { 
        this.visibleChannelItem(channel) 
      }) 
    } 
    .columnsTemplate('1fr 1fr 1fr 1fr') 
    .columnsGap(16) 
    .rowsGap(16) 
    .width('100%') 
    .height(Math.ceil(this.visibleChannels.length / 4) * (16 + this.itemWidth*0.6)) 
    .padding({ left: 22, right: 22 }) 
    .backgroundColor(Color.White) 
    // 设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem 
    .editMode(this.canEdit) 
    //是否支持动画。当前支持GridItem拖拽动画。仅在滚动模式下(只设置rowsTemplate、columnsTemplate其中一个)支持动画 
    .supportAnimation(true) 
    //设置为parallelGesture:会同时触发当前父组件与子组件的手势事件 
    .parallelGesture( 
      LongPressGesture() 
        .onAction(() => { 
          this.canEdit = true; 
          console.info("==Text==LongPressGesture===onAction===this.canEdit:"+this.canEdit); 
        }), 
      GestureMask.Normal 
    ) 
    //第一次拖拽此事件绑定的组件时,触发回调。 
    .onItemDragStart((event: ItemDragInfo, itemIndex: number) => { 
      console.info("onItemDragStart") 
      let channel = this.visibleChannels[itemIndex]//当前被拖拽的元素 
      return this.visibleChannelItem(channel) //设置拖拽过程中显示的图片。 
    }) 
    //绑定此事件的组件可作为拖拽释放目标,当在本组件范围内停止拖拽行为时,触发回调。 
    .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { 
      // isSuccess=false时,说明drop的位置在grid外部;insertIndex > length时,说明有新增元素的事件发生 
      if (!isSuccess || insertIndex >= this.visibleChannels.length) { 
        return 
      } 
      //交换数组中的拖拽的两个元素的位置 
      this.changeIndex(itemIndex, insertIndex) 
    }) 
  } 
  //网格组件 
  @Builder visibleChannelItem(channel: string) { 
    GridItem() { 
      Text(channel) 
        .fontSize(15) 
        .fontColor(0xFF383838) 
        .margin({ top: 9, right: 9 }) 
        .width(this.itemWidth - 9) 
        .height(this.itemWidth*0.6 - 9) 
        .backgroundColor(0xFFF9F9F9) 
        .textAlign(TextAlign.Center) 
    } 
    .width(this.itemWidth) 
    .height(this.itemWidth*0.6) 
  } 
}
  • 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.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
分享
微博
QQ
微信
回复
2024-08-05 12:22:53


相关问题
HarmonyOS Grid拖动排序长按冲突
1129浏览 • 2回复 待解决
HarmonyOS 拖动手势距离的单位是什么
498浏览 • 1回复 待解决
Image长按拖动如何关闭
2732浏览 • 1回复 待解决
HarmonyOS 是否有新闻频道管理的案例
523浏览 • 1回复 待解决
HarmonyOS webview自定义长按手势事件
584浏览 • 1回复 待解决
HarmonyOS 如何实现长按点击功能
635浏览 • 1回复 待解决
长按短按如何同时共存不冲突
598浏览 • 1回复 待解决
WebList嵌套手势冲突问题
1347浏览 • 1回复 待解决