22

Goal

Im trying to create a view with an animated shrinking header with a tabview that contains tabs with scrolling content. See Image.

Setup

I'm using react-navigation with a TabNavigator. The header is a component with a fixed height, currently above the TabNavigator. The header is fixed above the tabs all the time taking precious space.

Tried Approaches

  • I've tried Janic Duplessis Blog Post but I can't get it to work because of the tabs.

  • I've also tried implementing it with two ScrollView/FlatList: One wrapped around the whole view and one wrapping the content but I can't get react native to propagate the scroll edge is reached.

The desired effect is the same as in the Google Play store.

Mobile Screenshot Google play

2
  • Are you using a specific library for your header / tabs? What is the current behaviour of your implementation? AFAIK it isn't a good idea to nest ScrollViews.
    – mjduminy
    May 23, 2017 at 9:26
  • @mduminy Added some current implementation details about the current setup :) May 24, 2017 at 15:39

2 Answers 2

18

I managed to implement this with Animated.diffClamp - you can check out the code here. I've written an article explaining the code more in detail here

*I'm using the native-base package for UI components (to simplify UI code), but the animation interpolations/definitions are the same with or without it.

Demo

1
  • 2
    @AndiGu I followed your tutorial and I am getting a bug on slowscroll of the flatlist.When scroll slowly, the layout is flickering on Android. Jul 20, 2019 at 5:00
3

You could achieve this by controlling the animated scroll actions yourself using the RN-Animated component. This isn't exactly what you asked for but it can easily be played with to get the right results and you really only need to use the renderScrollComponent method of any list view to capture this same thing... so... Here's some code:

The Code

constructor(props) {
    super(props);

    this.headerHeight = 150;

    this.state = {
        scrollY: new Animated.Value(0),
    };
}

render() {
    return (
        <View style={[
                styles.container,
                {
                    width: this.props.display.width,
                    height: this.props.display.height,
                }
            ]}>

            { this.renderScrollView() }
            { this.renderListHeader() }

        </View>
    );
}


renderListHeader() {
    return (
        <Animated.View
            style={[
                styles.headerContainer,
                {
                    width: this.props.display.width,
                    height: this.state.scrollY.interpolate({
                        inputRange: [0, this.headerHeight],
                        outputRange: [this.headerHeight, this.headerHeight / 2],
                        extrapolate: 'clamp',
                    }),
                }
            ]}>

            <Text style={[bodyFontStyle, styles.testingText]}>
                I am a test! 
            </Text>
        </Animated.View>
    );
}


renderScrollView() {
    return (
        <Animated.ScrollView
            bounces={false}
            scrollEventThrottle={1}
            showsVerticalScrollIndicator={false}
            showsHorizontalScrollIndicator={false}
            onScroll={Animated.event( [{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }] )}>

            <Animated.View
                style={[
                    styles.innerScrollContainer,
                    {
                        height: 800,
                        width: this.props.display.width,
                        marginTop: this.headerHeight,
                    }
                ]}>

                <Text style={[bodyFontStyle, styles.innerContainerText]}>
                    Look ma! No Hands!
                </Text>
            </Animated.View>
        </Animated.ScrollView>
    );
}


const styles = StyleSheet.create({
    container: {
        position: 'relative',
        backgroundColor: 'transparent',
    },
    headerContainer: {
        position: 'absolute',
        top: 0, left: 0,
        overflow: 'hidden',
        backgroundColor: 'green',
   },
   innerScrollContainer: {
        position: 'relative',
        backgroundColor: 'white',
   },
   innerContainerText: { color: 'black' },
   testingText: { color: 'black' },
});

I'm sure you get the idea of what this code is trying to do... but just incase heres a breakdown.

Code Breakdown

I use the this.props.display.width / height props to send down the system level width/height of the current device screen width / height, but if you prefer flex use that instead.

You don't necessarily need a hardcoded or static header size, just a central place (i.e. class level or JS module level) to use it in your class code.

Lastly, just because I interpolated the scroll data to the height of the box, doesn't mean you have to. In the example you show it looks more like you would want to translate the box upward instead of shrinking it's height.

5
  • Thanks @JeremiStadler I don't have an account there and RN Playground is no more, so much appreciated.
    – GoreDefex
    Aug 3, 2017 at 18:19
  • Good idea but the header is only expanding when reaching the top of the list, not when scrolling upwards at the bottom. Also: No need for a expo account :) Aug 17, 2017 at 16:33
  • @JeremiStadler With a bit of tweaking this code could easily be made to do that.
    – GoreDefex
    Aug 17, 2017 at 16:34
  • Since this code can do what you need, and you just have to tinker with it to do so, can you please select this answer as the correct one once you get it working? Thanks
    – GoreDefex
    Aug 17, 2017 at 16:37
  • i will try out it
    – lutakyn
    Mar 28, 2019 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.