HarmonyOS 鸿蒙Next 列表滑动惯性处理

HarmonyOS 鸿蒙Next 列表滑动惯性处理

目前用List实现横向翻页效果, 发现我做额外设置的时候, 一次滑动之后, 因为滑动过快会划过好多页, 用List提供的friction参数进行控制, 但是设置之后效果很不理想, 有时候出现还是会一次滑动多页, 但是有时候又会滑动之后没有翻页, 而且friction参数不是一个运动相关的属性值, 无法直接设置一个明确的值来满足所有的滑动场景. Scroller也有类似参数speedLimit控制, 但是效果也不理想.

现在想要实现那种一次翻一页的效果,类似Swiper的翻页效果,参数该怎么调整. Swiper因为只提供showNext和showPrevious 无法满足业务场景


更多关于HarmonyOS 鸿蒙Next 列表滑动惯性处理的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

可以看下这个demo实现 

[@Entry](/user/Entry)
[@Component](/user/Component)
struct ListExample {
[@State](/user/State) arr: number[] = [0, 1, 2, 3, 4]
private scroller: ListScroller = new ListScroller()
private currentIndex: number = 0 // 当前index
private touchDownX: number = 0 // 标记触摸开始坐标x ,使用场景Axis.Horizontal
private touchDownY: number = 0 // 标记触摸开始坐标y ,使用场景Axis.Vertical
private touchMoveX: number = 0 // 标记拖拽过程坐标x ,使用场景Axis.Horizontal
private touchMoveY: number = 0 // 标记拖拽过程坐标y ,使用场景Axis.Vertical
private isReserve: boolean = false // 标记是否回撤不滚动到对应index
private axis: Axis = Axis.Horizontal // list方向
private isFling = false
build() {
Column() {
List({ space: 0, initialIndex: 0, scroller: this.scroller }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text('' + item)
.fontSize(16)
.textAlign(TextAlign.Center)
.borderRadius(10)
}.height('100%').width('100%').backgroundColor(0xFFFFFF)
}, (item: string) => item)

} .listDirection(this.axis) .scrollBar(BarState.Off) .edgeEffect(EdgeEffect.None) .nestedScroll({scrollBackward:NestedScrollMode.PARENT_FIRST,scrollForward:NestedScrollMode.PARENT_FIRST}) .width(‘100%’) .scrollSnapAlign(ScrollSnapAlign.CENTER) .onScrollIndex((start: number, end: number, center: number) => { console.log('fxm start = ’ + start + 'end = ’ + end + 'center = ’ + center) this.currentIndex = center }) .onScroll( (scrollState: ScrollState)=>{ if(scrollState === ScrollState.Fling) this.isFling = true else this.isFling = false })

.onTouch((event: TouchEvent) => { let touchObject: TouchObject = event.touches[0] if (touchObject.type === TouchType.Down) { this.onTouchDown(touchObject) } if (touchObject.type === TouchType.Up) { this.onTouchUp(touchObject) } if (touchObject.type === TouchType.Move) { this.onTouchMove(touchObject) } }) }

}

onTouchDown(touchObject: TouchObject) { this.touchDownX = touchObject.x this.touchDownY = touchObject.y } onTouchMove(touchObject: TouchObject) { this.touchMoveY = touchObject.y this.touchMoveX = touchObject.x if (this.axis === Axis.Vertical) { if (Math.abs(touchObject.y - this.touchDownY) < 50) this.isReserve = true else { if (touchObject.y < this.touchDownY) { this.isReserve = touchObject.y > this.touchMoveY } else { this.isReserve = touchObject.y < this.touchMoveY } } } else { if (Math.abs(touchObject.x - this.touchDownX) < 50) this.isReserve = true else { if (touchObject.x < this.touchDownX) { this.isReserve = touchObject.x > this.touchMoveX } else { this.isReserve = touchObject.x < this.touchMoveX } } } }

onTouchUp(touchObject: TouchObject) { if(this.isFling) return if (this.axis === Axis.Vertical) { if (touchObject.y < this.touchDownY) this.scroller.scrollToIndex(this.isReserve ? this.currentIndex : this.currentIndex + 1, true, ScrollAlign.CENTER) else this.scroller.scrollToIndex(this.isReserve ? this.currentIndex : this.currentIndex - 1, true, ScrollAlign.CENTER) } else { if (touchObject.x < this.touchDownX) this.scroller.scrollToIndex(this.isReserve ? this.currentIndex : this.currentIndex + 1, true, ScrollAlign.CENTER) else this.scroller.scrollToIndex(this.isReserve ? this.currentIndex : this.currentIndex - 1, true, ScrollAlign.CENTER) } } }<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

更多关于HarmonyOS 鸿蒙Next 列表滑动惯性处理的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙系统中,Next列表的滑动惯性处理是提升用户体验的重要一环。这通常涉及到对列表组件(如ScrollList或RecycleList)的滚动事件进行细致管理和优化。

为实现滑动惯性效果,你可以利用鸿蒙系统提供的滚动监听接口(如OnScrollListener)和动画处理API。首先,你需要捕获用户的滚动操作,记录滚动的起始速度和位置。在用户停止滚动后,根据记录的初始速度,使用动画框架(如Animator)来模拟滚动惯性的效果,即让列表继续平滑滚动一段距离后慢慢停止。

在这个过程中,可以调整动画的插值器(Interpolator)来更精确地控制滚动速度和加速度的变化,使惯性效果更加自然。此外,还可以根据用户操作的不同(如快速滑动或轻拂)来调整惯性动画的参数,以达到更好的交互体验。

请注意,在实现时,要处理好滑动边界的情况,避免列表在惯性滚动时超出有效范围。同时,确保动画处理的高效性,避免对系统性能造成不必要的负担。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部