mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Fix horizontal ScrollView resting past its left edge after a fling on Android 14
## Summary: On Android 14, a horizontal `ScrollView` resting at its left edge, dragged further left and released while moving, comes to rest 6 dp past that edge and stays there until it is touched again: the content shows a gap and the view reports a negative offset. In right-to-left layout the left edge is the reading end, so a list flung toward its end sticks the same way. `ReactHorizontalScrollView.fling()` always starts its own overscroll fling (`minX = 0`, `overX = width / 2`), where the platform's `HorizontalScrollView.fling()` absorbs a fling into an edge effect that is still pulled. `computeScroll()` clamps the overscroll to `mOverflingDistance` and springs back, but Android 14's `consumeFlingInStretch()` feeds every spring-back step into the stretch the drag is still pulling, so the scroller finishes at 0 and the view stays at −6 dp. Android 15 adds the guard that prevents it (`if (scrollX < 0 || scrollX > getScrollRange()) return unconsumed;`), and Android 13's `HorizontalScrollView` has no `consumeFlingInStretch`. From Android 12, where the platform's fling absorbs into a pulled edge, this hands a fling that heads into the left edge the view rests at to `super.fling()`, so the platform absorbs it into the edge effect as it does for a plain `HorizontalScrollView`. Every other fling, and every fling before Android 12, keeps React Native's own path. The vertical `ScrollView` is not affected: `ScrollView.onTouchEvent` releases through `flingWithNestedDispatch`, which absorbs a fling into an edge the view rests at and never calls `fling()`. ## Changelog: [ANDROID] [FIXED] - Fix horizontal ScrollView resting past its left edge after a fling on Android 14 ## Test Plan: - New `ReactHorizontalScrollViewFlingTest` (Robolectric, `sdk = 34`) drags a horizontal ScrollView at its left edge further left and releases it. On `main` it rests at `scrollX = -6` (`expected:<[0]> but was:<[-6]>`); with this change at 0. A second case flings into the content, so the drag is shown to produce a real fling. Robolectric's `ShadowEdgeEffect` turns edge effects off on API 31+, so the test supplies a shadow that keeps the stretch a device uses. - `ReactHorizontalScrollViewFlingTest`, `ReactScrollViewTouchCancelTest` and `ReactScrollViewPropertyTest` pass under `./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest -PenableWarningsAsErrors=true` (16 tests), and ktfmt, run with the flags `scripts/format-kotlin.js` passes it, leaves both files unchanged. - RNTester on an Android 14 (API 34) emulator: after the same fling, `Item 0` rests 18 px right of where it sat without this change and where it sat with it — [before/after](https://github.com/react/react-native/pull/59020#issuecomment-6134366035). Differential Revision: D131751400 Pull Request resolved: https://github.com/react/react-native/pull/59020
This commit is contained in:
1 parent
502f112fa3
commit
718b6150b1
2 files changed
+151
-1
No files matched your search
+4
-1
@@ -858,7 +858,10 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
|
||||
velocityX
|
||||
}
|
||||
|
||||
if (pagingEnabled) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && scrollX <= 0 && correctedVelocityX < 0) {
|
||||
// From Android 12 the platform absorbs this fling; an overscroll here sticks on Android 14.
|
||||
super.fling(correctedVelocityX)
|
||||
} else if (pagingEnabled) {
|
||||
flingAndSnap(correctedVelocityX)
|
||||
} else if (scroller != null) {
|
||||
// FB SCROLLVIEW CHANGE
|
||||
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
// The deprecated APIs exercised here remain public and required by this Robolectric setup.
|
||||
@file:Suppress("DEPRECATION")
|
||||
|
||||
package com.facebook.react.views.scroll
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Build
|
||||
import android.os.Looper
|
||||
import android.os.SystemClock
|
||||
import android.util.DisplayMetrics
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.widget.EdgeEffect
|
||||
import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext
|
||||
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
|
||||
import com.facebook.react.uimanager.DisplayMetricsHolder
|
||||
import com.facebook.react.uimanager.ThemedReactContext
|
||||
import com.facebook.react.uimanager.UIManagerHelper
|
||||
import com.facebook.react.uimanager.events.EventDispatcher
|
||||
import com.facebook.soloader.SoLoader
|
||||
import com.facebook.testutils.shadows.ShadowSoLoader
|
||||
import java.time.Duration
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.MockedStatic
|
||||
import org.mockito.Mockito.mockStatic
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.mock
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
|
||||
/** Verifies where a horizontal ScrollView comes to rest after a drag that ends in a fling. */
|
||||
@SuppressLint("DeprecatedClass", "DeprecatedMethod")
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(
|
||||
sdk = [34],
|
||||
shadows =
|
||||
[ShadowSoLoader::class, ReactHorizontalScrollViewFlingTest.ShadowStretchEdgeEffect::class],
|
||||
)
|
||||
class ReactHorizontalScrollViewFlingTest {
|
||||
|
||||
/** Robolectric turns every edge effect off; a device stretches the edge a drag pulls past. */
|
||||
@Implements(EdgeEffect::class)
|
||||
class ShadowStretchEdgeEffect {
|
||||
@Implementation(minSdk = Build.VERSION_CODES.S)
|
||||
fun getCurrentEdgeEffectBehavior(): Int = EDGE_EFFECT_TYPE_STRETCH
|
||||
}
|
||||
|
||||
private lateinit var themedContext: ThemedReactContext
|
||||
private lateinit var uiManagerHelperMock: MockedStatic<UIManagerHelper>
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
ReactNativeFeatureFlagsForTests.setUp()
|
||||
SoLoader.setInTestMode()
|
||||
val context = createTestReactApplicationContext()
|
||||
themedContext = ThemedReactContext(context, context, null, -1)
|
||||
DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics())
|
||||
uiManagerHelperMock = mockStatic(UIManagerHelper::class.java)
|
||||
uiManagerHelperMock
|
||||
.`when`<EventDispatcher?> { UIManagerHelper.getEventDispatcher(any()) }
|
||||
.thenReturn(mock())
|
||||
}
|
||||
|
||||
@After
|
||||
fun teardown() {
|
||||
uiManagerHelperMock.close()
|
||||
DisplayMetricsHolder.setScreenDisplayMetrics(null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFlingIntoLeftEdgeAtRestStaysAtLeftEdge() {
|
||||
val view = createAtLeftEdge()
|
||||
|
||||
dragAndRelease(view, fromX = DRAG_START_X, toX = DRAG_END_X)
|
||||
settle(view)
|
||||
|
||||
assertThat(view.scrollX).isEqualTo(0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFlingFromLeftEdgeIntoContentScrollsPastTheDrag() {
|
||||
val view = createAtLeftEdge()
|
||||
|
||||
dragAndRelease(view, fromX = DRAG_END_X, toX = DRAG_START_X)
|
||||
settle(view)
|
||||
|
||||
assertThat(view.scrollX).isGreaterThan((DRAG_END_X - DRAG_START_X).toInt())
|
||||
}
|
||||
|
||||
private fun createAtLeftEdge(): ReactHorizontalScrollView {
|
||||
val view = ReactHorizontalScrollViewManager().createViewInstance(themedContext)
|
||||
val content = View(themedContext)
|
||||
view.addView(content)
|
||||
content.layout(0, 0, CONTENT_WIDTH, VIEWPORT_SIZE)
|
||||
view.layout(0, 0, VIEWPORT_SIZE, VIEWPORT_SIZE)
|
||||
assertThat(view.scrollX).isEqualTo(0)
|
||||
return view
|
||||
}
|
||||
|
||||
private fun dragAndRelease(view: ReactHorizontalScrollView, fromX: Float, toX: Float) {
|
||||
val downTime = SystemClock.uptimeMillis()
|
||||
val stepX = (toX - fromX) / DRAG_STEPS
|
||||
view.onTouchEvent(obtainTouch(downTime, 0, MotionEvent.ACTION_DOWN, fromX))
|
||||
for (step in 1 until DRAG_STEPS) {
|
||||
view.onTouchEvent(obtainTouch(downTime, step, MotionEvent.ACTION_MOVE, fromX + stepX * step))
|
||||
}
|
||||
view.onTouchEvent(obtainTouch(downTime, DRAG_STEPS, MotionEvent.ACTION_UP, toX))
|
||||
}
|
||||
|
||||
private fun obtainTouch(downTime: Long, step: Int, action: Int, x: Float): MotionEvent =
|
||||
MotionEvent.obtain(downTime, downTime + step * STEP_MILLIS, action, x, TOUCH_Y, 0)
|
||||
|
||||
private fun settle(view: ReactHorizontalScrollView) {
|
||||
repeat(SETTLE_FRAMES) {
|
||||
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(FRAME_MILLIS))
|
||||
view.computeScroll()
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
// EdgeEffect.TYPE_STRETCH, which the SDK hides.
|
||||
const val EDGE_EFFECT_TYPE_STRETCH = 1
|
||||
const val VIEWPORT_SIZE = 1000
|
||||
const val CONTENT_WIDTH = 5000
|
||||
const val TOUCH_Y = 500f
|
||||
const val DRAG_START_X = 100f
|
||||
const val DRAG_END_X = 500f
|
||||
const val DRAG_STEPS = 4
|
||||
const val STEP_MILLIS = 10L
|
||||
const val FRAME_MILLIS = 16L
|
||||
const val SETTLE_FRAMES = 180
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user