When considering implement custom pull-down refresh components, first of all to be clear, this considered a hack program. Not to say how good are the custom, but custom drop-down refresh in
android
case there will be slight Caton (I realize this way). So try or use a small program that comes with it.This is achieved mainly with reference to Alan Gangster components .
Method to realize
We will page is divided into the following structure
The normal time drop-down refresh region height of 0
.
Then we pass scroll-view
the scrollToUpeper
event to mark the beginning to decline.
By touchstart
to record the initial position of our fingers.
By listening to the list of areas of the touchmove
event, to continuously calculate the distance of finger movement.
touchend
Event to trigger the update event.
Our pages are divided into several states:
enum refreshStatus {
INIT,
PULL_DOWN,
READY_REFRESH,
LOADING,
DONE
}
复制代码
Process substantially as follows:
-
After the page is loaded
INIT
state. -
When triggered
scroll-view
thescrollToUpeper
event, we recorded a flagisUpper
, inscroll-view
thescroll
event trigger,isUpper
denotedfalse
-
When we list the top of the page and then slip the drop-down, enter the page
PULL_DOWN
state. Usually set up an effective drop-down distance callvalidHeight
, when less than the height of the drop-downvalidHeight
when you release your finger this time, the page does not refresh the rebound. -
As we continue to pull down from more than
validHeight
at this time to enter theREADY_REFRESH
state. -
In the
READY_REFRESH
open state when the finger, refresh the page, enter theLOADING
state. -
After the page refreshes, enter the
DONE
state, becomes a refresh area height0
We also need to set a maximum height of the drop-down, as well as in the READY_REFRESH
state, after the finger is released to the maximum height of rebound refresh region.
At this point we can write touchend
, touchstart
, touchmove
the code is as follows :( detailed code )
handleTouchMove(e) {
const curTouch = e.touches[0]
const moveY = (curTouch.pageY - this.lastTouchY) * .3
if(
!this.isUpper ||
moveY < 0 ||
moveY > 2 * this.maxHeight ||
this.state.refreshStatu === refreshStatus.LOADING
) {
return
}
if(moveY < this.validHeight) {
this.setState({
refreshHeight: moveY,
refreshStatu: refreshStatus.PULL_DOWN
})
} else {
this.setState({
refreshHeight: moveY,
refreshStatu: refreshStatus.READY_REFRESH
})
}
}
handleTouchStart(e) {
const curTouch = e.touches[0]
this.lastTouchY = curTouch.pageY
}
handleTouchEnd() {
this.lastTouchY = 0
if(this.state.refreshStatu === refreshStatus.READY_REFRESH) {
this.setState({
refreshStatu: refreshStatus.LOADING,
refreshHeight: this.maxHeight
})
if (this.props.onRefresh) {
this.props.onRefresh()
}
} else {
this.setState({
refreshHeight: 0
})
}
}
handleScrollToUpeper() {
this.isUpper = true
}
handleScroll() {
this.isUpper = false
}
复制代码
In addition, we also need to add on the page calls the disableScroll: true
configuration to solve problems along the entire page ios decline.
Contrast native pull-down refresh
-
There will be a slight Caton, ios and simulator experience better next android.
-
Local pages can pull down to refresh. Refreshing style can also be customized.
Epilogue
In fact, in ios
case there is a way to achieve, is to use ios
the scroll-view
while also pulled the top of the previous drop-down (rubber band effect), this time we can refresh region on scroll-view
but the area above. Certainly better than ever performance computing height is better.
But there are two problems:
-
Page no longer topical, because topical use, then you need to disable page scrolling, which also followed the disabled ios rubber band effect, so that we can not use the drop-down refresh
-
There is a slight rebound effect after the finger is released, although harmless, but still feel very strange.
This implementation also next time I posted, you can refer to Allen's blog , he said, in more detail, I mainly refer to his code to achieve.
Reproduced in: https: //juejin.im/post/5cf488e06fb9a07f0052c53a