HarmonyOS swiper的disableSwipe所传的state不会马上生效,需要在一次滑动事件结束后才能生效
interface OffsetRemain {
offsetRemain: number
}
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
private scroller = new Scroller()
@State
private refreshOffset: number = 0
private refreshThreshold: number = 100
// 当次滑动的总偏移量,需要滑动开始时重置
private scrollOffset: number = 0.0
@State
private refreshing: boolean = false
@State
innerScrollEnabled: boolean = true
swiperController = new SwiperController()
build() {
Stack() {
Scroll(this.scroller) {
Swiper(this.swiperController) {
ForEach([1, 2, 3, 4], (item: number) => {
Column() {
Text('Page ' + item).fontSize(30)
Text('Hello World').fontSize(30)
}.width('100%').height('100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center).backgroundColor('#f00')
})
}
.effectMode(EdgeEffect.None)
.nestedScroll(SwiperNestedScrollMode.SELF_FIRST)
.width('100%')
.height('100%')
.loop(false)
.vertical(true)
.indicator(false)
.disableSwipe(!this.innerScrollEnabled)
.enabled(this.innerScrollEnabled)
// .enabled(this.innerScrollEnabled)
}
.edgeEffect(EdgeEffect.None)
.enablePaging(true)
.height('100%')
.width('100%')
.onScrollFrameBegin((offset: number, state: ScrollState) => {
console.info('onScrollFrameBegin')
return this.onScrollFrameBegin(offset, state)
})
.onScrollStart(() => {
this.onScrollStart()
})
.onScrollStop(() => {
this.onScrollStop()
})
.onWillScroll((xOffset: number, yOffset: number, scrollState: ScrollState, scrollSource: ScrollSource) => {
return this.onWillScroll(xOffset, yOffset, scrollState, scrollSource)
})
Column() {
Text(`pull to refresh ${this.refreshOffset}`)
}.width('100%').height(100).position({
top: this.refreshing? 0 : this.refreshOffset - 100,
left: 0,
right: 0
}).backgroundColor('#0f0')
}.height('100%').width('100%')
}
private onScrollStart() {
// reset this.scrollOffset when scroll start
console.info('onScrollStart')
this.scrollOffset = 0
}
private onScrollStop() {
// console.info('onScrollStop', this.refreshOffset)
// // check if should trigger refresh when scroll stop
// if (this.refreshOffset >= this.refreshThreshold) {
// animateTo({duration: 100}, ()=>{
// this.refreshing = true
// })
//
// // do refresh
// setTimeout(() => {
// this.refreshing = false
// this.refreshOffset = 0
// }, 5000)
// }
}
private onScrollFrameBegin(offset: number, state: ScrollState): OffsetRemain {
if (state != ScrollState.Scroll) {
return { offsetRemain: offset }
}
this.scrollOffset += offset
console.info('onScrollFrameBegin', offset, ScrollState[state], this.scrollOffset)
// update inner scroll enabled status
if (this.scrollOffset < 0.001) {
console.info("disable inner scroll")
this. innerScrollEnabled = false
} else {
console.info("enable inner scroll")
this.innerScrollEnabled = true
}
const atBegin = this.scroller.currentOffset().yOffset <= 0.001
const atEnd = this.scroller.isAtEnd()
// 当前滑动的方向,以当前滑动的偏移量的正负来判断
const scrollForward = offset < 0.001
const scrollBackward = offset > 0.001
// 如果当前在顶部且向前滑动,则触发下拉刷新,更新 this.refreshOffset, 当 this.refreshOffset 大于等于 this.refreshThreshold 时触发刷新且后续的下拉距离也减半
if (atBegin) {
this.refreshOffset = -this.scrollOffset
}
return { offsetRemain: offset }
}
// onWillScroll 在 overscroll 时, xOffset / yOffset 永远为 0
private onWillScroll(xOffset: number, yOffset: number, scrollState: ScrollState,
scrollSource: ScrollSource): void | OffsetResult {
const atBegin = this.scroller.currentOffset().yOffset <= 0.001
const atEnd = this.scroller.isAtEnd()
const pullForward = yOffset > 0.001
const pullBackward = yOffset < 0.001
console.info('atBegin', atBegin, 'atEnd', atEnd, 'pullForward', pullForward, 'pullBackward', pullBackward,
'yOffset', yOffset)
// 如果向前拉并当前已经在顶部,则触发下拉刷新,更新 this.refreshOffset
// 如果当前在顶部且向后拉,则刷新 this.refreshOffset 直到其值为 0
if (atBegin && pullForward) {
this.refreshOffset = yOffset
} else if (atBegin && pullBackward) {
this.refreshOffset = Math.max(0, this.refreshOffset + yOffset)
}
}
}
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
Dev studio需要运行两次才能 生效最新修改的代码
2455浏览 • 1回复 待解决
HarmonyOS 动画必须搭配@State修饰的变量才能生效吗?
202浏览 • 1回复 待解决
点击事件,@State 页面未生效,在线等
3100浏览 • 0回复 待解决
HarmonyOS Swiper的disableSwipe问题
168浏览 • 1回复 待解决
HarmonyOS 热重载功能,偶尔失效,需要重启才能生效
288浏览 • 1回复 待解决
HarmonyOS 父组件的@state变量更新后子组件不生效
171浏览 • 1回复 待解决
HarmonyOS 刷新Swiper里的数据时,index下标还是上一次的值
422浏览 • 1回复 待解决
HarmonyOS @state修饰的变量作为判断条件 控制onTouchIntercept事件拦截,不能实时生效
91浏览 • 1回复 待解决
HarmonyOS 每秒执行一次的函数
470浏览 • 2回复 待解决
HarmonyOS swiper预览图片有一个图片放大场景,放大后需要滑动事件image组件自己消费
264浏览 • 1回复 待解决
Web组件的onKeyEvent键盘事件不生效
2212浏览 • 1回复 待解决
HarmonyOS textinput组件的text属性通过set state方式不生效
169浏览 • 1回复 待解决
HarmonyOS Swiper使用时Indicator设置位置不生效
913浏览 • 1回复 待解决
HarmonyOS 页面在前后台的监听,如果多个地方多次设置监听,是都能监听到状态变化还是只有最后一次生效
117浏览 • 1回复 待解决
HarmonyOS Swiper设置 .prevMargin('10%') .nextMargin('10%')不生效
273浏览 • 1回复 待解决
HarmonyOS App第一次启动,需要弹出授权隐私页面吗?
502浏览 • 1回复 待解决
HarmonyOS 如何只取消某一次的监听
239浏览 • 1回复 待解决
HarmonyOS Radio的onChange方法只会触法一次
145浏览 • 1回复 待解决
#鸿蒙学习大百科#如何解决全屏后margin不会生效的问题?
408浏览 • 1回复 待解决
HarmonyOS 视频播放结束后,重播,需要点击重播点两遍才能重播
806浏览 • 1回复 待解决
有没有一次编译,多端部署的资料
610浏览 • 1回复 待解决
HarmonyOS Swiper滑动到最后一页,再次滑动是否有相应事件?
909浏览 • 1回复 待解决
为什么Web组件的onKeyEvent键盘事件不生效
2490浏览 • 1回复 待解决
状态变量更新的时候,手势还是被之前的组件消费,没有作用在swipe上,所以要抬手,再重新滑动一次swiper, swiper才能接受到滑动事件,开始滑动