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) 
  } 
}
分享
微博
QQ
微信
回复
2024-08-05 12:22:53
相关问题
Image长按拖动如何关闭
1805浏览 • 1回复 待解决
HarmonyOS 如何是实现手势密码功能
148浏览 • 1回复 待解决
WebList嵌套手势冲突问题
646浏览 • 1回复 待解决
拖动实现列表重新排序
585浏览 • 1回复 待解决
Image组件无法设置长按事件
1512浏览 • 1回复 待解决
长按实现各类振动效果
576浏览 • 1回复 待解决
Charles 抓包 网络长按无法修改
356浏览 • 1回复 待解决
页面上下拖动时怎么隐藏键盘
667浏览 • 1回复 待解决
HarmonyOS 有无对应的长按事件
98浏览 • 1回复 待解决
HarmonyOS如何实现自定义布局内置手势
134浏览 • 0回复 待解决
长按事件如何重复触发
1826浏览 • 1回复 待解决