用户3096723
用户3096723
  • 发布:2026-07-03 11:38
  • 更新:2026-07-03 11:38
  • 阅读:15

如何解决拖拽功能和长按功能的冲突问题

分类:HBuilder

问题现象
在List组件中,单个ListItem在同时设置拖拽功能与长按功能时,实际运行之后会产生冲突,如何解决单个ListItem拖拽功能和长按功能的冲突问题?

效果预览
点击放大

背景知识
支持统一拖拽提供了一种通过鼠标或手势触屏传递数据的机制,即从一个组件位置拖出数据并将其拖入到另一个组件位置,以触发响应。
长按手势通过长按屏幕触发长按手势事件。
由于拖拽事件和长按手势都需要通过长按来触发,因此这种组合手势类型会出现冲突,例如List组件中每个ListItem都设置单独的长按事件时,拖拽功能和长按手势功能就会出现冲突。
Grid网格容器,由“行”和“列”分割的单元格所组成,通过指定“项目”所在的单元格做出各种各样的布局。
滑动手势PanGesture,能够提供自定义拖拽事件的功能。
解决方案
由于在ListItem中设置单独的长按事件会与拖拽事件产生冲突,因此建议放弃List本身的拖拽事件,使用滑动手势PanGesture,自行实现拖拽过程的逻辑,然后再通过组合手势的功能,去实现其他的长按事件。
import curves from '@ohos.curves';

@Entry
@Component
struct Page {
// 元素数组
@State numbers: number[] = [];
// 多列
private str: string = '';
row: number = 4;
// 元素数组中最后一个元素的索引
@State lastIndex: number = 0;
@State dragItem: number = -1;
@State scaleItem: number = -1;
item: number = -1;
private dragRefOffsetX: number = 0;
private dragRefOffsetY: number = 0;
@State offsetX: number = 0;
@State offsetY: number = 0;
private FIX_VP_X: number = 108;
private FIX_VP_Y: number = 120;

aboutToAppear() {
for (let i = 1; i <= 36; i++) {
this.numbers.push(i);
}
this.lastIndex = this.numbers.length - 1;
// 多列
for (let i = 0; i < this.row; i++) {
this.str = this.str + '1fr ';
}
}

itemMove(index: number, newIndex: number): void {
console.info('index:' + index + ' newIndex:' + newIndex);
if (!this.isDraggable(newIndex)) {
return;
}
let tmp = this.numbers.splice(index, 1);
this.numbers.splice(newIndex, 0, tmp[0]);
}

// 向下滑
down(index: number): void {
if (!this.isDraggable(index + this.row)) {
return;
}
this.offsetY -= this.FIX_VP_Y;
this.dragRefOffsetY += this.FIX_VP_Y;
// 多列
this.itemMove(index, index + this.row);
}

// 向下滑(右下角为空)
down2(index: number): void {
if (!this.isDraggable(index + 3)) {
return;
}
this.offsetY -= this.FIX_VP_Y;
this.dragRefOffsetY += this.FIX_VP_Y;
this.itemMove(index, index + 3);
}

// 向上滑
up(index: number): void {
if (!this.isDraggable(index - this.row)) {
return;
}
this.offsetY += this.FIX_VP_Y;
this.dragRefOffsetY -= this.FIX_VP_Y;
this.itemMove(index, index - this.row);
}

// 向左滑
left(index: number): void {
if (!this.isDraggable(index - 1)) {
return;
}
this.offsetX += this.FIX_VP_X;
this.dragRefOffsetX -= this.FIX_VP_X;
this.itemMove(index, index - 1);
}

// 向右滑
right(index: number): void {
if (!this.isDraggable(index + 1)) {
return;
}
this.offsetX -= this.FIX_VP_X;
this.dragRefOffsetX += this.FIX_VP_X;
this.itemMove(index, index + 1);
}

// 向右下滑
lowerRight(index: number): void {
if (!this.isDraggable(index + this.row + 1)) {
return;
}
this.offsetX -= this.FIX_VP_X;
this.dragRefOffsetX += this.FIX_VP_X;
this.offsetY -= this.FIX_VP_Y;
this.dragRefOffsetY += this.FIX_VP_Y;
this.itemMove(index, index + this.row + 1);
}

// 向右上滑
upperRight(index: number): void {
if (!this.isDraggable(index - (this.row - 1))) {
return;
}
this.offsetX -= this.FIX_VP_X;
this.dragRefOffsetX += this.FIX_VP_X;
this.offsetY += this.FIX_VP_Y;
this.dragRefOffsetY -= this.FIX_VP_Y;
this.itemMove(index, index - (this.row - 1));
}

// 向左下滑
lowerLeft(index: number): void {
if (!this.isDraggable(index + (this.row - 1))) {
return;
}
this.offsetX += this.FIX_VP_X;
this.dragRefOffsetX -= this.FIX_VP_X;
this.offsetY -= this.FIX_VP_Y;
this.dragRefOffsetY += this.FIX_VP_Y;
this.itemMove(index, index + (this.row - 1));
}

// 向左上滑
upperLeft(index: number): void {
if (!this.isDraggable(index - (this.row + 1))) {
return;
}
this.offsetX += this.FIX_VP_X;
this.dragRefOffsetX -= this.FIX_VP_X;
this.offsetY += this.FIX_VP_Y;
this.dragRefOffsetY -= this.FIX_VP_Y;
this.itemMove(index, index - (this.row + 1));
}

// 通过元素的索引,控制对应元素是否能移动排序
isDraggable(index: number): boolean {
console.info(index: ${index});
return index > -1; // 恒成立,所有元素均可移动排序
}
https://pastebin.com/FQ7caXPg
https://pastebin.com/qrG8cLtE
https://pastebin.com/NDkKX7GA
https://pastebin.com/v7SbJUa2
https://pastebin.com/ckwh85Uv
https://pastebin.com/FyQiZnSa
https://pastebin.com/eJTdnrt3
https://pastebin.com/FSynV6Lb
https://pastebin.com/DAj2WRVR
https://pastebin.com/2Y0LWg8C
https://pastebin.com/U3rE0GN7
https://pastebin.com/5SqdHMZn
https://pastebin.com/3vZs9u6Y
https://pastebin.com/6SHV25A8
https://pastebin.com/cxcm4039
https://pastebin.com/Rgdfcp9d
https://pastebin.com/g8Thr4Mg
https://pastebin.com/rmwrNJji
https://pastebin.com/yKbAsYCa
https://pastebin.com/dRdM2X9u
https://pastebin.com/ecff1hqL
https://pastebin.com/UGKhTWQ8
https://pastebin.com/s5hexmMG
https://pastebin.com/vixNaxtq
https://pastebin.com/jM99sFyZ
https://pastebin.com/enua3UEF
https://pastebin.com/d0QccunL
https://pastebin.com/NW6ahtVi
https://pastebin.com/HdFmVJDd
https://pastebin.com/dvqz627c
https://pastebin.com/2dkeMADC
https://pastebin.com/H61nZMVs
https://pastebin.com/VAJYkWis
https://pastebin.com/3Y7xVCzF
https://pastebin.com/XWH0bcEB
https://pastebin.com/0yZbjU38
https://pastebin.com/KT9GU4Xj
https://pastebin.com/6JyqmT7e
https://pastebin.com/3fFG8u4J
https://pastebin.com/jLbepB1x
https://pastebin.com/fcqxvaXX
https://pastebin.com/3yKQCFzT
https://pastebin.com/Gpz5Yc8A
https://pastebin.com/KUjNqvfq
https://pastebin.com/JRp0SxQ5
https://pastebin.com/dAeJ7KxQ
https://pastebin.com/Y59qi6iT
https://pastebin.com/hsBpJYrt
https://pastebin.com/2wNNccE8
https://pastebin.com/dHCqmGeK
https://pastebin.com/5zYAQpvu
https://pastebin.com/dN3bshkc
https://pastebin.com/cFSUYDNN
https://pastebin.com/yWKUSPZz
https://pastebin.com/pjyMPVgp
https://pastebin.com/iASRBbGz
https://pastebin.com/zrVfHyC0
https://pastebin.com/Lg6i3qBs
https://pastebin.com/C9y6bEfC
https://pastebin.com/3xhPr5Uz
https://pastebin.com/u6fHcaUM
https://pastebin.com/GTjsW1uh
https://pastebin.com/15xfuW7r
https://pastebin.com/ATU9sQ6v
https://pastebin.com/a2mCY3Zz
https://pastebin.com/ntnWiFcr
https://pastebin.com/AH45r30t
https://pastebin.com/H0AQ1Dww
https://pastebin.com/diHn40ki
https://pastebin.com/CZH3ckGE
https://pastebin.com/UxAfGRQt
https://pastebin.com/FLNsUiG0
https://pastebin.com/AAWktRKj
https://pastebin.com/sKm7XvTE
https://pastebin.com/gvp4Q7sh
https://pastebin.com/xuCUMBur
https://pastebin.com/2espHLP1
https://pastebin.com/64pjxFAc
https://pastebin.com/J5fQPN5U
https://pastebin.com/J0rJg6DX
https://pastebin.com/QtWnkBDp
https://pastebin.com/SeGa4TkD
https://pastebin.com/WTwjcNN1
https://pastebin.com/MAqkmJXT
https://pastebin.com/sct8XVCM
https://pastebin.com/hkT7CqNH
https://pastebin.com/f7pCY395
https://pastebin.com/ZSbSpbL6
https://pastebin.com/Ug0RKLfK
https://pastebin.com/CV8Ercz3
https://pastebin.com/guYntd9W
https://pastebin.com/Dz3vpsZz
https://pastebin.com/0QCcxGWy
https://pastebin.com/Nd37dhtk
https://pastebin.com/yf59y43d
https://pastebin.com/nj7W9tyx
https://pastebin.com/5JCZtmRJ
https://pastebin.com/wagqVUtS
https://pastebin.com/1X65Yjc3
https://pastebin.com/TtXircBz
https://pastebin.com/fTwWHfEe
https://pastebin.com/zYrag51J
https://pastebin.com/E9icvGyK
https://pastebin.com/2cXwRAxL
https://pastebin.com/w0GGQRhc
https://pastebin.com/53sfuRZK
https://pastebin.com/90uQ7V15
https://pastebin.com/ghzaUYMF
https://pastebin.com/Nnpgvdes
https://pastebin.com/Y8tuCtaz
https://pastebin.com/iWx1huat
https://pastebin.com/FdQgitkv
https://pastebin.com/8amig0td
https://pastebin.com/yzcTvFfu
https://pastebin.com/68HwXpv5
https://pastebin.com/KLAWLeKM
https://pastebin.com/dMUbkzF2
https://pastebin.com/g6rLz6au
https://pastebin.com/trd44WK2
https://pastebin.com/cQ8audPK
https://pastebin.com/wRuQcX47
https://pastebin.com/nyzk75PP
https://pastebin.com/a52FLwqg
https://pastebin.com/ifwAEKs8
https://pastebin.com/7hsBC6eM
https://pastebin.com/pLAWFQ8U
https://pastebin.com/j70c8uBX
https://pastebin.com/ymKQsh4f
https://pastebin.com/4dqNaX6T
https://pastebin.com/HSdANUrQ
https://pastebin.com/Nh8sivuq
https://pastebin.com/q55w0w2i
https://pastebin.com/wXf37h3E
https://pastebin.com/RhAxX63P
https://pastebin.com/
https://pastebin.com/d0ftXeii
https://pastebin.com/w7cKXeMc
https://pastebin.com/vJQZ8zzg
https://pastebin.com/NV6uSXAB
https://pastebin.com/aDZckQqR
https://pastebin.com/LECR3S3x
https://pastebin.com/cp78R0ry
https://pastebin.com/WD0P7hDC
https://pastebin.com/iA0h7MsB
https://pastebin.com/509YHhv2
https://pastebin.com/sR22DfaA
https://pastebin.com/fkwWBgB1
https://pastebin.com/xu1tZaib
https://pastebin.com/0e9mbjm1
https://pastebin.com/8ygNAi7Q
https://pastebin.com/q10N4BVV
https://pastebin.com/E5a1VSUL
https://pastebin.com/N4rt7xkq
https://pastebin.com/KLKahxYp
https://pastebin.com/aAhka9TS
https://pastebin.com/cFzzqVdL
https://pastebin.com/TdF2uE9H
https://pastebin.com/sAwMjAbi
https://pastebin.com/DZNFtzZq
https://pastebin.com/tHjy4Dqz
https://pastebin.com/7nGK9sVT
https://pastebin.com/uh2XxG80
https://pastebin.com/AnR5ZVHR
https://pastebin.com/Y3RLNXTu
https://pastebin.com/H1QvABLE
https://pastebin.com/gu3H4ZCC
https://pastebin.com/6stL1Xpp
https://pastebin.com/yx5inydp
https://pastebin.com/hyiiCV2A
https://pastebin.com/rgCkSxnQ
https://pastebin.com/smR119Kz
https://pastebin.com/H6GguYti
https://pastebin.com/zMupz2e0
https://pastebin.com/hA0jPvEB
https://pastebin.com/a617mt5H
https://pastebin.com/nkjG08Fw
https://pastebin.com/k6NKap1G
https://pastebin.com/mmiNsmtC
https://pastebin.com/i6V5V5dz
https://pastebin.com/GUvCDwNE
https://pastebin.com/Cd7LEAAE
https://pastebin.com/uN4xeDXm
https://pastebin.com/WT2yyehE
https://pastebin.com/3qxa54xX
https://pastebin.com/xdnPSK9i
https://pastebin.com/EEpWrErg
https://pastebin.com/MbUH0ZVm
https://pastebin.com/UfRH2vMY
https://pastebin.com/qcmM4qiM
https://pastebin.com/0NXAxPQR
https://pastebin.com/bXmTJhyf
https://pastebin.com/716Zr85T
https://pastebin.com/X0JFDTJ0
https://pastebin.com/gSc7vpdF
https://pastebin.com/LKUU1kbW
https://pastebin.com/XfbRxeZ7
https://pastebin.com/afMDqP8e
https://pastebin.com/QxJgJX0x
https://pastebin.com/WGrQcNAF
https://pastebin.com/Kbj0zKmQ

0 关注 分享

要回复文章请先登录注册