question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android: ScrollView content container doesn't animate to new layout

See original GitHub issue

Description

If the content in an Animated.ScrollView is using a layout transition, and transitioning from a larger height to a smaller height, the content container within the Animated.ScrollView jumps to the new height (instead of transitioning). This results in the content of the scroll view being clipped.

I’m only able to replicate this on Android, iOS seems to work fine.

Expected behavior

I would expect the layout of the content container to transition with the content on Android, as it does on iOS.

Actual behavior & steps to reproduce

https://user-images.githubusercontent.com/1010583/145732568-182c2f36-f68a-46d0-b55c-6691c94e9200.mp4

Snack or minimal code example

Note the layout prop on Animated.View below

import React, {useState} from 'react'
import {Button, SafeAreaView, StyleSheet} from 'react-native'
import Animated, {Easing, Layout} from 'react-native-reanimated'

export function randomIntFromInterval(min: number, max: number) {
  return Math.floor(Math.random() * (max - min + 1) + min)
}

const App = () => {
  const [height, setHeight] = useState(200)

  return (
    <SafeAreaView style={styles.container}>
      <Animated.ScrollView style={[styles.container]}>
        <Button
          title="Random height"
          onPress={() => {
            setHeight(randomIntFromInterval(200, 600))
          }}
        />
        <Animated.View
          layout={Layout.duration(240).easing(Easing.inOut(Easing.ease))} 
          style={{backgroundColor: 'teal', height}}
        />
      </Animated.ScrollView>
      )
    </SafeAreaView>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
})

export default App

Package versions

  • React Native: 0.66.3
  • React Native Reanimated: 2.3.0
  • NodeJS:
  • Xcode:
  • Java & Gradle:

Affected platforms

  • Android
  • iOS
  • Web

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
piaskowykcommented, Jan 4, 2022

Hey, I resolved the problem. You just need to use https://reactnative.dev/docs/scrollview#contentcontainerstyle

<Animated.ScrollView 
  contentContainerStyle={{ flex: 1 }} // <- this flex is important
>

Without this view was cropped by the parent component.

working code
import React, {useState} from 'react'
import {Button, SafeAreaView, StyleSheet} from 'react-native'
import Animated, {Easing, Layout} from 'react-native-reanimated'

export function randomIntFromInterval(min: number, max: number) {
  return Math.floor(Math.random() * (max - min + 1) + min)
}

const App = () => {
  const [height, setHeight] = useState(200)

  return (
    <SafeAreaView style={styles.container}>
      <Animated.ScrollView style={[styles.container]} 
        contentContainerStyle={{
          flex: 1
        }}
      >
        <Button
          title="Random height"
          onPress={() => {
            setHeight(randomIntFromInterval(200, 600))
          }}
        />
        <Animated.View
          layout={Layout.duration(240).easing(Easing.inOut(Easing.ease))} 
          style={{backgroundColor: 'teal', zIndex: 100, height}}
        />
      </Animated.ScrollView>
    </SafeAreaView>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
})

export default App;
0reactions
DigitalZebracommented, Jan 5, 2022

Thanks much! Will give this a shot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scrollview doesn't scroll after resizing it's child using animation
I have a LinearLayout inside a ScrollView. At some point I collapse and expand the LinearLayout. After that the scrollview does scroll. Any ......
Read more >
ScrollView - React Native
Component that wraps platform ScrollView while providing integration with touch locking "responder" system.
Read more >
Reveal or hide a view using animation - Android Developers
This animation is useful for situations where you want to switch content or views in your app. The crossfade animation shown here uses ......
Read more >
overscroll-behavior - CSS: Cascading Style Sheets | MDN
If you have overscroll-behavior: contain; selected, the outer container will not scroll unless you move your cursor out of the inner container ...
Read more >
Handling Scrolls with CoordinatorLayout - CodePath Cliffnotes
So long as the CoordinatorLayout is used as the primary layout, this animation effect will occur for you automatically. The floating action button...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found