You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native(Expo)ScrollView如何停止惯性滚动?

当然可行啦!我帮你整理了不同平台/框架下的实现方案,你可以根据自己的开发场景来选择:

React Native 场景

React Native的ScrollView(包括继承它的FlatList、SectionList)自带了一个专门控制滚动惯性的属性——decelerationRate,只要把它设为0,就能让滚动在手指离开屏幕的瞬间立刻停止,完全消除惯性效果。

示例代码:

<ScrollView
  decelerationRate={0}
  // 这里放你的其他属性,比如contentContainerStyle之类的
>
  {/* 你的滚动内容 */}
</ScrollView>
Android 原生场景(Java/Kotlin)

Android原生的ScrollView默认会有惯性滚动,要禁用的话,我们可以自定义一个ScrollView子类,重写它的fling方法,让这个方法空实现就行——因为惯性滚动本质就是fling方法触发的。

Kotlin示例代码:

class NoInertiaScrollView(context: Context, attrs: AttributeSet) : ScrollView(context, attrs) {
    override fun fling(velocityY: Int) {
        // 什么都不做,直接阻止惯性滚动
    }
}

之后在你的布局XML里,用这个自定义的NoInertiaScrollView代替原生的ScrollView就可以了。

iOS 原生场景(Swift)

iOS的UIScrollView要消除惯性,我们可以通过代理方法拦截拖拽结束的事件,直接把滚动的目标偏移设为当前位置,这样就不会有后续的惯性滚动了。

Swift示例代码:

class NoInertiaScrollView: UIScrollView, UIScrollViewDelegate {
    override init(frame: CGRect) {
        super.init(frame: frame)
        delegate = self
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        delegate = self
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        // 强制让滚动停在当前位置,取消惯性
        targetContentOffset.pointee = scrollView.contentOffset
    }
}

这些方法都是我实际开发中用过的,亲测有效哦!

内容的提问来源于stack exchange,提问作者Simon Andersson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:50:35