Here is an example of React Native Collapsible Toolbar. It was first introduced in Android Material Design and became very popular. In Collapsible Toolbar when screen renders for the first time we got the broad header which will help us to get the attention of the user on important details. As the user scrolls up header goes up with the screen and collapses itself because this time the focus of the user is on the data below the header and again when the user pulls the screen down it expands and become broad again.
In this example of Collapsible Toolbar, we are using Animated a React Native component which helps us to manage our toolbar. So let’s get started.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli
command line utility. Open the terminal and go to the workspace and run
Open App.js in any code editor and replace the code with the following code.
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | //This is an example code for React Native Collapsible Toolbar// import React, { Component } from 'react'; //import react in our code. import { ScrollView, StyleSheet, View, Animated, Text, Platform, } from 'react-native'; //import all the components we are going to use. const Header_Maximum_Height = 150; //Max Height of the Header const Header_Minimum_Height = 50; //Min Height of the Header export default class Mynewproject extends Component<{}> { constructor() { super(); this.AnimatedHeaderValue = new Animated.Value(0); } render() { const AnimateHeaderBackgroundColor = this.AnimatedHeaderValue.interpolate({ inputRange: [0, Header_Maximum_Height - Header_Minimum_Height], outputRange: ['#4286F4', '#00BCD4'], extrapolate: 'clamp', }); const AnimateHeaderHeight = this.AnimatedHeaderValue.interpolate({ inputRange: [0, Header_Maximum_Height - Header_Minimum_Height], outputRange: [Header_Maximum_Height, Header_Minimum_Height], extrapolate: 'clamp', }); return ( <View style={styles.MainContainer}> <ScrollView scrollEventThrottle={16} contentContainerStyle={{ paddingTop: Header_Maximum_Height }} onScroll={Animated.event([ { nativeEvent: { contentOffset: { y: this.AnimatedHeaderValue } } }, ])}> {/* Put all your Component here inside the ScrollView */} <Text style={styles.TextViewStyle}>Text</Text> <Text style={styles.TextViewStyle}> Input</Text> <Text style={styles.TextViewStyle}>Button</Text> <Text style={styles.TextViewStyle}>Card</Text> <Text style={styles.TextViewStyle}>CheckBox</Text> <Text style={styles.TextViewStyle}>Divider</Text> <Text style={styles.TextViewStyle}>Header</Text> <Text style={styles.TextViewStyle}>List Item</Text> <Text style={styles.TextViewStyle}>Pricing</Text> <Text style={styles.TextViewStyle}>Rating</Text> <Text style={styles.TextViewStyle}>Search Bar</Text> <Text style={styles.TextViewStyle}>Slider</Text> <Text style={styles.TextViewStyle}>Tile</Text> <Text style={styles.TextViewStyle}>Icon</Text> <Text style={styles.TextViewStyle}>Avatar</Text> </ScrollView> <Animated.View style={[ styles.Header, { height: AnimateHeaderHeight, backgroundColor: AnimateHeaderBackgroundColor, }, ]}> <Text style={styles.HeaderInsideText}> List of React Native Elements </Text> </Animated.View> </View> ); } } const styles = StyleSheet.create({ MainContainer: { flex: 1, paddingTop: Platform.OS == 'ios' ? 20 : 0, }, Header: { justifyContent: 'center', alignItems: 'center', position: 'absolute', left: 0, right: 0, top: Platform.OS == 'ios' ? 20 : 0, }, HeaderInsideText: { color: '#fff', fontSize: 18, textAlign: 'center', }, TextViewStyle: { textAlign: 'center', color: '#000', fontSize: 18, margin: 5, padding: 7, }, }); |
To Run the React Native App
Open the terminal again and jump into your project using.That was the React Native Collapsible Toolbar. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
How useful was this post?
Click on a star to rate us!
Average rating / 5. Vote count:
We are sorry that this post was not useful for you!
Let us improve this post!
Thanks for your feedback!