@@ -38,7 +38,7 @@ import Animated, { useSharedValue, withTiming, useAnimatedStyle, runOnJS } from
3838import { warn , hasOwn } from '@mpxjs/utils'
3939import useInnerProps , { getCustomEvent } from './getInnerListeners'
4040import useNodesRef , { HandlerRef } from './useNodesRef'
41- import { splitProps , splitStyle , useTransformStyle , useLayout , wrapChildren , extendObject , flatGesture , GestureHandler , HIDDEN_STYLE } from './utils'
41+ import { splitProps , splitStyle , useTransformStyle , useLayout , wrapChildren , extendObject , flatGesture , GestureHandler , HIDDEN_STYLE , useRunOnJSCallback } from './utils'
4242import { IntersectionObserverContext , ScrollViewContext } from './context'
4343import Portal from './mpx-portal'
4444
@@ -210,6 +210,15 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
210210 const { textStyle, innerStyle = { } } = splitStyle ( normalStyle )
211211
212212 const scrollViewRef = useRef < ScrollView > ( null )
213+
214+ const runOnJSCallbackRef = useRef ( {
215+ setEnableScroll,
216+ setScrollBounces,
217+ setRefreshing,
218+ onRefresh
219+ } )
220+ const runOnJSCallback = useRunOnJSCallback ( runOnJSCallbackRef )
221+
213222 useNodesRef ( props , ref , scrollViewRef , {
214223 style : normalStyle ,
215224 scrollOffset : scrollOptions ,
@@ -446,22 +455,6 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
446455 }
447456 }
448457
449- function onScrollTouchStart ( e : NativeSyntheticEvent < TouchEvent > ) {
450- const { bindtouchstart } = props
451- bindtouchstart && bindtouchstart ( e )
452- if ( enhanced ) {
453- binddragstart &&
454- binddragstart (
455- getCustomEvent ( 'dragstart' , e , {
456- detail : {
457- scrollLeft : scrollOptions . current . scrollLeft ,
458- scrollTop : scrollOptions . current . scrollTop
459- } ,
460- layoutRef
461- } , props )
462- )
463- }
464- }
465458 function onScrollTouchMove ( e : NativeSyntheticEvent < TouchEvent > ) {
466459 bindtouchmove && bindtouchmove ( e )
467460 if ( enhanced ) {
@@ -478,22 +471,6 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
478471 }
479472 }
480473
481- function onScrollTouchEnd ( e : NativeSyntheticEvent < TouchEvent > ) {
482- bindtouchend && bindtouchend ( e )
483- if ( enhanced ) {
484- binddragend &&
485- binddragend (
486- getCustomEvent ( 'dragend' , e , {
487- detail : {
488- scrollLeft : scrollOptions . current . scrollLeft || 0 ,
489- scrollTop : scrollOptions . current . scrollTop || 0
490- } ,
491- layoutRef
492- } , props )
493- )
494- }
495- }
496-
497474 function onScrollDrag ( e : NativeSyntheticEvent < NativeScrollEvent > ) {
498475 const { x : scrollLeft , y : scrollTop } = e . nativeEvent . contentOffset
499476 updateScrollOptions ( e , { scrollLeft, scrollTop } )
@@ -514,6 +491,35 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
514491 hasCallScrollToLower . current = false
515492 hasCallScrollToUpper . current = false
516493 onScrollDrag ( e )
494+ if ( enhanced ) {
495+ binddragstart &&
496+ binddragstart (
497+ getCustomEvent ( 'dragstart' , e , {
498+ detail : {
499+ scrollLeft : scrollOptions . current . scrollLeft ,
500+ scrollTop : scrollOptions . current . scrollTop
501+ } ,
502+ layoutRef
503+ } , props )
504+ )
505+ }
506+ }
507+
508+ function onScrollDragEnd ( e : NativeSyntheticEvent < NativeScrollEvent > ) {
509+ onScrollDrag ( e )
510+ if ( enhanced ) {
511+ // 安卓上如果触发了默认的下拉刷新,binddragend可能不触发,只会触发 binddragstart
512+ binddragend &&
513+ binddragend (
514+ getCustomEvent ( 'dragend' , e , {
515+ detail : {
516+ scrollLeft : scrollOptions . current . scrollLeft || 0 ,
517+ scrollTop : scrollOptions . current . scrollTop || 0
518+ } ,
519+ layoutRef
520+ } , props )
521+ )
522+ }
517523 }
518524
519525 // 处理刷新
@@ -587,7 +593,7 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
587593 'worklet'
588594 if ( enableScrollValue . value !== newValue ) {
589595 enableScrollValue . value = newValue
590- runOnJS ( setEnableScroll ) ( newValue )
596+ runOnJS ( runOnJSCallback ) ( 'setEnableScroll' , newValue )
591597 }
592598 }
593599
@@ -600,7 +606,7 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
600606 'worklet'
601607 if ( bouncesValue . value !== newValue ) {
602608 bouncesValue . value = newValue
603- runOnJS ( setScrollBounces ) ( newValue )
609+ runOnJS ( runOnJSCallback ) ( 'setScrollBounces' , newValue )
604610 }
605611 }
606612
@@ -649,19 +655,19 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
649655 if ( ( event . translationY > 0 && translateY . value < refresherThreshold ) || event . translationY < 0 ) {
650656 translateY . value = withTiming ( 0 )
651657 updateScrollState ( true )
652- runOnJS ( setRefreshing ) ( false )
658+ runOnJS ( runOnJSCallback ) ( 'setRefreshing' , false )
653659 } else {
654660 translateY . value = withTiming ( refresherHeight . value )
655661 }
656662 } else if ( event . translationY >= refresherHeight . value ) {
657663 // 触发刷新
658664 translateY . value = withTiming ( refresherHeight . value )
659- runOnJS ( onRefresh ) ( )
665+ runOnJS ( runOnJSCallback ) ( 'onRefresh' )
660666 } else {
661667 // 回弹
662668 translateY . value = withTiming ( 0 )
663669 updateScrollState ( true )
664- runOnJS ( setRefreshing ) ( false )
670+ runOnJS ( runOnJSCallback ) ( 'setRefreshing' , false )
665671 }
666672 } )
667673 . simultaneousWithExternalGesture ( scrollViewRef )
@@ -686,11 +692,9 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
686692 ref : scrollViewRef ,
687693 onScroll : enableSticky ? scrollHandler : onScroll ,
688694 onContentSizeChange : onContentSizeChange ,
689- bindtouchstart : ( ( enhanced && binddragstart ) || bindtouchstart ) && onScrollTouchStart ,
690695 bindtouchmove : ( ( enhanced && binddragging ) || bindtouchmove ) && onScrollTouchMove ,
691- bindtouchend : ( ( enhanced && binddragend ) || bindtouchend ) && onScrollTouchEnd ,
692696 onScrollBeginDrag : onScrollDragStart ,
693- onScrollEndDrag : onScrollDrag ,
697+ onScrollEndDrag : onScrollDragEnd ,
694698 onMomentumScrollEnd : onScrollEnd
695699 } ,
696700 ( simultaneousHandlers ? { simultaneousHandlers } : { } ) ,
0 commit comments