|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { View, StyleSheet, Platform, Animated, ScrollView, } from 'react-native'; |
| 3 | +import { List, ListItem, Left, Body, Right, Thumbnail, Text } from 'native-base'; |
| 4 | + |
| 5 | + |
| 6 | +const HEADER_MIN_HEIGHT = 0; |
| 7 | +const HEADER_MAX_HEIGHT = 300; |
| 8 | + |
| 9 | +const data = [ |
| 10 | + { |
| 11 | + title:"Rihanna Diamond", |
| 12 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 13 | + time: "3:43 pm", |
| 14 | + image : {uri: 'https://i.pravatar.cc/150?img=26'} |
| 15 | + }, |
| 16 | + { |
| 17 | + title:"Rihanna Needed Me", |
| 18 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 19 | + time: "3:43 pm", |
| 20 | + image : {uri: 'https://i.pravatar.cc/150?img=27'} |
| 21 | + }, |
| 22 | + { |
| 23 | + title:"Needed Me", |
| 24 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 25 | + time: "3:43 pm", |
| 26 | + image : {uri: 'https://i.pravatar.cc/150?img=28'} |
| 27 | + }, |
| 28 | + { |
| 29 | + title:"Rihanna Diamond", |
| 30 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 31 | + time: "3:43 pm", |
| 32 | + image : {uri: 'https://i.pravatar.cc/150?img=29'} |
| 33 | + }, |
| 34 | + { |
| 35 | + title:"Rihanna Diamond", |
| 36 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 37 | + time: "3:43 pm", |
| 38 | + image : {uri: 'https://i.pravatar.cc/150?img=30'} |
| 39 | + }, |
| 40 | + { |
| 41 | + title:"Rihanna Needed Me", |
| 42 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 43 | + time: "3:43 pm", |
| 44 | + image : {uri: 'https://i.pravatar.cc/150?img=28'} |
| 45 | + }, |
| 46 | + { |
| 47 | + title:"Rihanna Diamond", |
| 48 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 49 | + time: "3:43 pm", |
| 50 | + image : {uri: 'https://i.pravatar.cc/150?img=26'} |
| 51 | + }, |
| 52 | + { |
| 53 | + title:"Needed Me", |
| 54 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 55 | + time: "3:43 pm", |
| 56 | + image : {uri: 'https://i.pravatar.cc/150?img=27'} |
| 57 | + }, |
| 58 | + { |
| 59 | + title:"Rihanna Diamond", |
| 60 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 61 | + time: "3:43 pm", |
| 62 | + image : {uri: 'https://i.pravatar.cc/150?img=28'} |
| 63 | + }, |
| 64 | + { |
| 65 | + title:"Rihanna Diamond", |
| 66 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 67 | + time: "3:43 pm", |
| 68 | + image : {uri: 'https://i.pravatar.cc/150?img=27'} |
| 69 | + }, |
| 70 | + { |
| 71 | + title:"Needed Me", |
| 72 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 73 | + time: "3:43 pm", |
| 74 | + image : {uri: 'https://i.pravatar.cc/150?img=28'} |
| 75 | + }, |
| 76 | + { |
| 77 | + title:"Rihanna Needed Me", |
| 78 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 79 | + time: "3:43 pm", |
| 80 | + image : {uri: 'https://i.pravatar.cc/150?img=28'} |
| 81 | + }, |
| 82 | + { |
| 83 | + title:"Rihanna Diamond", |
| 84 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 85 | + time: "3:43 pm", |
| 86 | + image : {uri: 'https://i.pravatar.cc/150?img=26'} |
| 87 | + }, |
| 88 | + { |
| 89 | + title:"Rihanna Diamond", |
| 90 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 91 | + time: "3:43 pm", |
| 92 | + image : {uri: 'https://i.pravatar.cc/150?img=27'} |
| 93 | + }, |
| 94 | + { |
| 95 | + title:"Rihanna Needed Me", |
| 96 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 97 | + time: "3:43 pm", |
| 98 | + image : {uri: 'https://i.pravatar.cc/150?img=28'} |
| 99 | + }, |
| 100 | + { |
| 101 | + title:"Rihanna Diamond", |
| 102 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 103 | + time: "3:43 pm", |
| 104 | + image : {uri: 'https://i.pravatar.cc/150?img=27'} |
| 105 | + }, |
| 106 | + { |
| 107 | + title:"Needed Me", |
| 108 | + body : "Rihanna - baby, this is what you came for lightning strikes every time she moves and every body's watching her . . . ", |
| 109 | + time: "3:43 pm", |
| 110 | + image : {uri: 'https://i.pravatar.cc/150?img=28'} |
| 111 | + } |
| 112 | +] |
| 113 | + |
| 114 | +export default class AnimatedList extends Component { |
| 115 | + |
| 116 | + constructor() { |
| 117 | + super(); |
| 118 | + |
| 119 | + this.scrollYAnimatedValue = new Animated.Value(0); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + renderItem(item,index) |
| 125 | + { |
| 126 | + return ( |
| 127 | + <ListItem key={index} avatar onPress={()=>{}}> |
| 128 | + <Left> |
| 129 | + <Thumbnail source={item.image} /> |
| 130 | + </Left> |
| 131 | + <Body> |
| 132 | + <Text style={styles.avatarText}>{item.title}</Text> |
| 133 | + <Text style={styles.avatarText} note>{item.body}</Text> |
| 134 | + </Body> |
| 135 | + <Right> |
| 136 | + <Text style={styles.avatarText} note>{item.time}</Text> |
| 137 | + </Right> |
| 138 | + </ListItem>); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + render() { |
| 144 | + |
| 145 | + const headerHeight = this.scrollYAnimatedValue.interpolate( |
| 146 | + { |
| 147 | + inputRange: [(HEADER_MIN_HEIGHT - HEADER_MAX_HEIGHT), (HEADER_MAX_HEIGHT - HEADER_MIN_HEIGHT)], |
| 148 | + outputRange: [HEADER_MAX_HEIGHT+ (HEADER_MAX_HEIGHT - HEADER_MIN_HEIGHT), HEADER_MIN_HEIGHT], |
| 149 | + extrapolate: 'clamp' |
| 150 | + }); |
| 151 | + |
| 152 | + return ( |
| 153 | + <View style={styles.container} > |
| 154 | + <Animated.Image |
| 155 | + source={{url:"https://c1.staticflickr.com/7/6209/6126637255_23896dca42.jpg"}} |
| 156 | + style={[styles.animatedHeaderContainer, { height: headerHeight }]} |
| 157 | + /> |
| 158 | + <ScrollView |
| 159 | + contentContainerStyle={{ paddingTop: HEADER_MAX_HEIGHT }} |
| 160 | + scrollEventThrottle={()=>{}} |
| 161 | + onScroll={Animated.event( |
| 162 | + [{ nativeEvent: { contentOffset: { y: this.scrollYAnimatedValue } } }] |
| 163 | + )}> |
| 164 | + { |
| 165 | + <List style={{width:'100%'}}> |
| 166 | + {data.map((item,index)=>{ |
| 167 | + return this.renderItem(item,index); |
| 168 | + })} |
| 169 | + </List> |
| 170 | + } |
| 171 | + </ScrollView> |
| 172 | + |
| 173 | + </View> |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +const styles = StyleSheet.create( |
| 179 | + { |
| 180 | + container: { |
| 181 | + flex: 1, |
| 182 | + }, |
| 183 | + animatedHeaderContainer: { |
| 184 | + position: 'absolute', |
| 185 | + left: 0, |
| 186 | + right: 0, |
| 187 | + justifyContent: 'center', |
| 188 | + alignItems: 'center' |
| 189 | + }, |
| 190 | + headerText: { |
| 191 | + color: 'white', |
| 192 | + fontSize: 22 |
| 193 | + }, |
| 194 | + item: { |
| 195 | + backgroundColor: '#fff', |
| 196 | + borderBottomWidth:2, |
| 197 | + borderColor:"#eee", |
| 198 | + height: 45, |
| 199 | + justifyContent: 'center', |
| 200 | + alignItems: 'center' |
| 201 | + }, |
| 202 | + itemText: { |
| 203 | + color: 'black', |
| 204 | + fontSize: 16 |
| 205 | + } |
| 206 | + |
| 207 | + }); |
0 commit comments