Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScrollView weird behaviour when there are less items #42874

Closed
shreyanshgoel9 opened this issue Feb 5, 2024 · 22 comments
Closed

ScrollView weird behaviour when there are less items #42874

shreyanshgoel9 opened this issue Feb 5, 2024 · 22 comments
Assignees
Labels
Component: ScrollView Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Needs: Attention Issues where the author has responded to feedback.

Comments

@shreyanshgoel9
Copy link

shreyanshgoel9 commented Feb 5, 2024

Description

When the scrollview container is bigger than total item width, on scroll the items start sticking on right on android.
Tested on physical device as well

Screen.Recording.2024-02-05.at.8.20.01.PM.mov

Steps to reproduce

  1. Installed React Native macOs, android simulator

  2. Write this code
    <ScrollView horizontal={true} style={{backgroundColor: 'green'}}> <View style={{ height: 40, width: 50, marginRight: 20, backgroundColor: 'red', }} /> <View style={{ height: 40, width: 50, marginRight: 20, backgroundColor: 'red', }} /> </ScrollView>

  3. Scroll horizontal to reproduce

React Native Version

0.73.3

Affected Platforms

Runtime - Android

Output of npx react-native info

System:
  OS: macOS 14.1.2
  CPU: (8) arm64 Apple M1 Pro
  Memory: 87.98 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.12.1
    path: ~/.nvm/versions/node/v18.12.1/bin/node
  Yarn:
    version: 1.22.19
    path: ~/.nvm/versions/node/v18.12.1/bin/yarn
  npm:
    version: 8.19.2
    path: ~/.nvm/versions/node/v18.12.1/bin/npm
  Watchman:
    version: 2023.08.14.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.2
    path: /opt/homebrew/opt/ruby/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2022.3 AI-223.8836.35.2231.10811636
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 20.0.2
    path: /opt/homebrew/opt/openjdk/bin/javac
  Ruby:
    version: 3.2.2
    path: /opt/homebrew/opt/ruby/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.3
    wanted: 0.73.3
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

Stacktrace or Logs

-

Reproducer

snack.expo.dev/@uvrubel/rn-scrollview-horizontal

Screenshots and Videos

No response

@github-actions github-actions bot added Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. labels Feb 5, 2024
Copy link

github-actions bot commented Feb 5, 2024

⚠️ Missing Reproducible Example
ℹ️ We could not detect a reproducible example in your issue report. Please provide either:
  • If your bug is UI related: a Snack
  • If your bug is build/update related: use our Reproducer Template. A reproducer needs to be in a GitHub repository under your username.

@github-actions github-actions bot added Component: ScrollView Needs: Attention Issues where the author has responded to feedback. and removed Needs: Author Feedback labels Feb 5, 2024
@Myst1024
Copy link

Myst1024 commented Feb 13, 2024

Running into the same issue, using React Native 0.73.4. I have only been able to reproduce on physical Android devices.

@RakaDoank
Copy link

I also have this issue. I can reproduce this in physical Android device (API 33) and Android simulator (API 34). So far, no problem on iOS simulator.

I actually wanted to write this earlier. I remember i get this issue since i upgraded the RN to 0.73.0 from 0.72.7, until now currently on 0.73.4.

@kemplaw
Copy link

kemplaw commented Feb 17, 2024

Running into the same issue, using React Native 0.72.10.

@ulyavrubel
Copy link

Running into the same issue, using React Native 0.73.4. Reproducible on Android only

Steps to Reproduce

     <ScrollView horizontal>
       ...
      </ScrollView>

Demo

https://snack.expo.dev/@uvrubel/rn-scrollview-horizontal

screen-20240227-111557.mp4

@cortinico cortinico added Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. and removed Needs: Triage 🔍 Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. labels Mar 5, 2024
@Titozzz
Copy link
Collaborator

Titozzz commented Mar 12, 2024

Can confirm this is not something that was here before (but cannot easily bisect).
I can reproduce on 0.73.2 and 0.73.6

@Titozzz
Copy link
Collaborator

Titozzz commented Mar 12, 2024

After testing with Snack, doesn't happen with SDK 49 but happens on 50. Looking through the commits now.

@Titozzz
Copy link
Collaborator

Titozzz commented Mar 12, 2024

30c7e9d is the culprit!

@NickGerleman
Copy link
Contributor

Whoah, that's a crazy effect.

The interesting logic here is gated on:

mReactScrollViewScrollState.getLayoutDirection() == LAYOUT_DIRECTION_RTL

@Titozzz is this path being hit on physical device?

@Titozzz
Copy link
Collaborator

Titozzz commented Mar 12, 2024

I'll have to check tomorrow ! I'm home now 😬

@Titozzz
Copy link
Collaborator

Titozzz commented Mar 13, 2024

30c7e9d#diff-067e69cd88e616e9605533a4be612685b607a7d1cd29f854ee77d713113cb170R612-R614 is the change that breaks things

@NickGerleman
Copy link
Contributor

NickGerleman commented Mar 13, 2024

Huh. That was to fix another bug, where flinging to the edge would sometimes start scrolling backwards (#38236 is one report, though I think I saw others after the fact).

The version clamp was based on a linked Android bug report being fixed, and seeing incorrect behavior in emulator. So I think there might be a couple possibilities:

  1. Different behavior on another Android version. @Titozzz do you remember version in emulator vs device where you could or couldn’t repro this? If we can only repro on some devices, I wonder if this might be the case.
  2. There are more bugs here, that the issue ended up masking.

@Regin99
Copy link

Regin99 commented Mar 18, 2024

30c7e9d#diff-067e69cd88e616e9605533a4be612685b607a7d1cd29f854ee77d713113cb170R612-R614 is the change that breaks things

Even if i fully downgrade android scroll logic (ReactAndroid/src/.../views/scroll) to 0.72.4 version, bug is still reproduced. Interesting fact, that shopify's flashlist reproduce this bug too.
Are u sure that this changes breaks things?

Tested on emulator 30, 34 api RN 0.73.5

@Titozzz
Copy link
Collaborator

Titozzz commented Mar 18, 2024

Yep you are using prefab probably!
https://reactnative.dev/contributing/how-to-build-from-source#update-your-project-to-build-from-source
Did you do that ?

@Titozzz
Copy link
Collaborator

Titozzz commented Mar 18, 2024

This is the line that breaks it for me 30c7e9d#diff-067e69cd88e616e9605533a4be612685b607a7d1cd29f854ee77d713113cb170R612-R614.

(Verified with breakpoints)

@Regin99
Copy link

Regin99 commented Mar 18, 2024

Yep you are using prefab probably! https://reactnative.dev/contributing/how-to-build-from-source#update-your-project-to-build-from-source Did you do that ?

Yeah, my mistake. I'm forgot bout prefabs. Thank you

NickGerleman added a commit to NickGerleman/react-native that referenced this issue Mar 20, 2024
…Less Length than Scrolling View

Summary:
Fixes facebook#42874

## Sumary

D9405703 added some custom logic for Flings, to support FlatList scenarios where content is being added on the fly, during Fling animation. This works by allowing start Fling to not have bounds, then correcting/cancelling Fling when overscroll happens over a bound that would normally be allowed.

This has some math to try to determine max content length, and will clamp to this when scrolling over it. This logic is incorrect when content length is less than scrollview length, and we end up snapping to a negative offset.

This change adds clamping, so that we don't snap to negative position in horizontal scroll view. This clamping was already indirectly present on vertical scroll view. https://www.internalfb.com/code/fbsource/[b43cdf9b2fec71f5341ec8ff2d47e28a066f052e]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java?lines=609

## Test Plan

Above issue no longer reproes. Flinging while content is being added to horizontal FlatList still works correctly.

Changelog:
[Android][Fixed] - Fix Android Horizontal ScrollView OverScroll Fling when Content View Less Length than Scrolling View

Differential Revision: D55108818
@hjun555
Copy link

hjun555 commented Mar 21, 2024

same issue here, any solutions?

@Regin99
Copy link

Regin99 commented Mar 21, 2024

same issue here, any solutions?

Look above

huntie pushed a commit that referenced this issue Mar 25, 2024
…ScrollView length (#43563)

Summary:
Pull Request resolved: #43563

Fixes #42874

## Sumary

D9405703 added some custom logic for Flings, to support FlatList scenarios where content is being added on the fly, during Fling animation. This works by allowing start Fling to not have bounds, then correcting/cancelling Fling when overscroll happens over a bound that would normally be allowed.

This has some math to try to determine max content length, and will clamp to this when scrolling over it. This logic is incorrect when content length is less than scrollview length, and we end up snapping to a negative offset.

This change adds clamping, so that we don't snap to negative position in horizontal scroll view. This clamping was already indirectly present on vertical scroll view. https://www.internalfb.com/code/fbsource/[b43cdf9b2fec71f5341ec8ff2d47e28a066f052e]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java?lines=609

## Test Plan

Above issue no longer reproes. Flinging while content is being added to horizontal FlatList still works correctly.

Changelog:
[Android][Fixed] - Fix Android HorizontalScrollView fling when content length less than ScrollView length

Reviewed By: javache

Differential Revision: D55108818

fbshipit-source-id: 7cf0065f9f92832cc2606d1c7534fc150407b9c9
alfonsocj pushed a commit that referenced this issue Apr 12, 2024
…ScrollView length (#43563)

Summary:
Pull Request resolved: #43563

Fixes #42874

## Sumary

D9405703 added some custom logic for Flings, to support FlatList scenarios where content is being added on the fly, during Fling animation. This works by allowing start Fling to not have bounds, then correcting/cancelling Fling when overscroll happens over a bound that would normally be allowed.

This has some math to try to determine max content length, and will clamp to this when scrolling over it. This logic is incorrect when content length is less than scrollview length, and we end up snapping to a negative offset.

This change adds clamping, so that we don't snap to negative position in horizontal scroll view. This clamping was already indirectly present on vertical scroll view. https://www.internalfb.com/code/fbsource/[b43cdf9b2fec71f5341ec8ff2d47e28a066f052e]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java?lines=609

## Test Plan

Above issue no longer reproes. Flinging while content is being added to horizontal FlatList still works correctly.

Changelog:
[Android][Fixed] - Fix Android HorizontalScrollView fling when content length less than ScrollView length

Reviewed By: javache

Differential Revision: D55108818

fbshipit-source-id: 7cf0065f9f92832cc2606d1c7534fc150407b9c9
@baez97
Copy link

baez97 commented Apr 15, 2024

In case someone needs a fix without upgrading, setting contentContainerStyle to full width worked for me

<ScrollView horizontal contentContainerStyle={{ width: "100%" }}>

douglowder pushed a commit to react-native-tvos/react-native-tvos that referenced this issue Apr 22, 2024
…ScrollView length (#43563)

Summary:
Pull Request resolved: facebook/react-native#43563

Fixes facebook/react-native#42874

## Sumary

D9405703 added some custom logic for Flings, to support FlatList scenarios where content is being added on the fly, during Fling animation. This works by allowing start Fling to not have bounds, then correcting/cancelling Fling when overscroll happens over a bound that would normally be allowed.

This has some math to try to determine max content length, and will clamp to this when scrolling over it. This logic is incorrect when content length is less than scrollview length, and we end up snapping to a negative offset.

This change adds clamping, so that we don't snap to negative position in horizontal scroll view. This clamping was already indirectly present on vertical scroll view. https://www.internalfb.com/code/fbsource/[b43cdf9b2fec71f5341ec8ff2d47e28a066f052e]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java?lines=609

## Test Plan

Above issue no longer reproes. Flinging while content is being added to horizontal FlatList still works correctly.

Changelog:
[Android][Fixed] - Fix Android HorizontalScrollView fling when content length less than ScrollView length

Reviewed By: javache

Differential Revision: D55108818

fbshipit-source-id: 7cf0065f9f92832cc2606d1c7534fc150407b9c9
@RobinBobin
Copy link

Was it fixed in both 0.73 and 0.74? What are the versions, please?
I still see this bug in 0.73.4.

@NickGerleman
Copy link
Contributor

Was it fixed in both 0.73 and 0.74? What are the versions, please? I still see this bug in 0.73.4.

Available in 0.73.7+

@Arjun059
Copy link

Arjun059 commented Oct 29, 2024

Quick Fix;
contentContainerStyle={{flexGrow: 1}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: ScrollView Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Needs: Attention Issues where the author has responded to feedback.
Projects
None yet