HarmonyOS 鸿蒙Next list的item拖拽到底部无法向上滚动
HarmonyOS 鸿蒙Next list的item拖拽到底部无法向上滚动
list的item拖拽到底部的时候,list无法向上滚动
2 回复
参考demo如下:
import curves from '@ohos.curves';
import Curves from '@ohos.curves'
// xxx.ets
@Entry
@Component
struct ListItemExample {
@State private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@State dragItem: number = -1
@State scaleItem: number = -1
@State neighborItem: number = -1
@State neighborScale: number = -1
private dragRefOffset: number = 0
@State offsetX: number = 0
@State offsetY: number = 0
private ITEM_INTV: number = 120
private listScroller: ListScroller = new ListScroller()
// list组件规格
private listArea: Area = {
width: 0,
height: 0,
position: {},
globalPosition: {}
}
scaleSelect(item: number): number {
if (this.scaleItem == item) {
return 1.05
} else if (this.neighborItem == item) {
return this.neighborScale
} else {
return 1
}
}
itemMove(index: number, newIndex: number): void {
let tmp = this.arr.splice(index, 1)
this.arr.splice(newIndex, 0, tmp[0])
}
build() {
Stack() {
List({ space: 20, initialIndex: 0, scroller: this.listScroller }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text('' + item)
.width('100%')
.height(100)
.fontSize(16)
.textAlign(TextAlign.Center)
.borderRadius(10)
.backgroundColor(0xFFFFFF)
.shadow(this.scaleItem == item ? {
radius: 70,
color: '#15000000',
offsetX: 0,
offsetY: 0
} :
{
radius: 0,
color: '#15000000',
offsetX: 0,
offsetY: 0
})
.animation({ curve: Curve.Sharp, duration: 300 })
}
.margin({ left: 12, right: 12 })
.scale({ x: this.scaleSelect(item), y: this.scaleSelect(item) })
.zIndex(this.dragItem == item ? 1 : 0)
.translate(this.dragItem == item ? { y: this.offsetY } : { y: 0 })
.gesture(
// 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
GestureGroup(GestureMode.Sequence,
LongPressGesture({ repeat: true })
.onAction((event?: GestureEvent) => {
animateTo({ curve: Curve.Friction, duration: 300 }, () => {
this.scaleItem = item
})
})
.onActionEnd(() => {
animateTo({ curve: Curve.Friction, duration: 300 }, () => {
this.scaleItem = -1
})
}),
PanGesture({ fingers: 1, direction: null, distance: 0 })
.onActionStart(() => {
this.dragItem = item
this.dragRefOffset = 0
})
.onActionUpdate((event: GestureEvent) => {
this.offsetY = event.offsetY - this.dragRefOffset
// console.log('Y:' + this.offsetY.toString())
this.neighborItem = -1
let index = this.arr.indexOf(item)
let curveValue = Curves.initCurve(Curve.Sharp)
let value: number = 0
//根据位移计算相邻项的缩放
if (this.offsetY < 0) {
value = curveValue.interpolate(-this.offsetY / this.ITEM_INTV)
this.neighborItem = this.arr[index-1]
this.neighborScale = 1 - value / 20;
console.log('neighborScale:' + this.neighborScale.toString())
} else if (this.offsetY > 0) {
value = curveValue.interpolate(this.offsetY / this.ITEM_INTV)
this.neighborItem = this.arr[index+1]
this.neighborScale = 1 - value / 20;
}
// 防止offsetY触发越界逻辑
if (index === 0) {
this.offsetY = Math.max(0, this.offsetY)
} else if (index === this.arr.length - 1) {
this.offsetY = Math.min(0, this.offsetY)
}
// 获取手指信息
let fingerInfo = event.fingerList[0]
let clickPercentY =
(fingerInfo.globalY - Number(this.listArea.globalPosition.y)) / Number(this.listArea.height)
let curListOffset = this.listScroller.currentOffset()
// 根据触摸区域百分比确定自动上滑/下滑
if (clickPercentY > 0.8 && !this.listScroller.isAtEnd()) {
// 触摸到最底部加速滚动
let scrollVelocity = clickPercentY > 0.9 ? 4 : 2
this.listScroller.scrollTo({ xOffset: 0, yOffset: curListOffset.yOffset += scrollVelocity })
this.dragRefOffset -= scrollVelocity
} else if (clickPercentY < 0.2 && (index !== 0 || this.offsetY > 0)) {
// 触摸到最顶部加速滚动
let scrollVelocity = clickPercentY < 0.1 ? 4 : 2
this.listScroller.scrollTo({ xOffset: 0, yOffset: curListOffset.yOffset -= scrollVelocity })
this.dragRefOffset += scrollVelocity
}
//根据位移交换排序
if (this.offsetY > this.ITEM_INTV / 2) {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.offsetY -= this.ITEM_INTV
this.dragRefOffset += this.ITEM_INTV
this.itemMove(index, index + 1)
})
} else if (this.offsetY < -this.ITEM_INTV / 2) {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.offsetY += this.ITEM_INTV
this.dragRefOffset -= this.ITEM_INTV
this.itemMove(index, index - 1)
})
}
})
.onActionEnd((event: GestureEvent) => {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.dragItem = -1
this.neighborItem = -1
})
animateTo({
curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150
}, () => {
this.scaleItem = -1
})
})
)
.onCancel(() => {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.dragItem = -1
this.neighborItem = -1
})
animateTo({
curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150
}, () => {
this.scaleItem = -1
})
})
)
}, (item: number) => item.toString())
}
.onAreaChange((oldValue: Area, newValue: Area) => {
this.listArea = newValue
})
}.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
}
}
更多关于HarmonyOS 鸿蒙Next list的item拖拽到底部无法向上滚动的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙系统中,针对Next list的item拖拽到底部无法向上滚动的问题,通常与列表的滚动事件处理或滚动边界判定有关。
-
检查滚动事件监听: 确保为列表正确设置了滚动事件监听器,并且监听器能够正确处理滚动的边界条件。特别是拖拽到底部时,监听器应能识别到滚动范围的限制,并允许用户通过反向操作(如向上拖动)来恢复滚动。
-
调整滚动边界判定: 检查列表的滚动边界判定逻辑,确保在拖拽到底部时,系统不会错误地认为已经到达不可滚动的区域。可能需要调整滚动边界的判定条件,以允许在底部附近有一定的缓冲区,用于触发反向滚动。
-
更新UI组件: 如果使用的是第三方UI组件库,确保组件库版本是最新的,并且已经修复了相关的滚动问题。
-
测试与验证: 在不同的设备和系统版本上测试该功能,确保问题不是由特定的硬件或软件环境引起的。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html