react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import React, {Component} from 'react';
  2. import {
  3. Text,
  4. View,
  5. ScrollView,
  6. TouchableOpacity,
  7. TouchableWithoutFeedback,
  8. StyleSheet,
  9. ListView,
  10. Image
  11. } from 'react-native';
  12. import heroes from './heroes';
  13. import {SharedElementTransition} from 'react-native-navigation';
  14. import * as heroStyles from './styles';
  15. const LOREM_IPSUM = 'Lorem ipsum dolor sit amet, ius ad pertinax oportere accommodare, an vix civibus corrumpit referrentur. Te nam case ludus inciderint, te mea facilisi adipiscing. Sea id integre luptatum. In tota sale consequuntur nec. Erat ocurreret mei ei. Eu paulo sapientem vulputate est, vel an accusam intellegam interesset. Nam eu stet pericula reprimique, ea vim illud modus, putant invidunt reprehendunt ne qui.';
  16. const hashCode = function(str) {
  17. var hash = 15;
  18. for (var ii = str.length - 1; ii >= 0; ii--) {
  19. hash = ((hash << 5) - hash) + str.charCodeAt(ii);
  20. }
  21. return hash;
  22. };
  23. export default class ListScreen extends Component {
  24. static navigatorStyle = {
  25. ...heroStyles.navigatorStyle
  26. };
  27. static navigatorButtons = {
  28. rightButtons: [
  29. {
  30. title: 'Add',
  31. icon: require('../../../img/navicon_add.png'),
  32. id: 'add',
  33. showAsAction: 'always'
  34. },
  35. {
  36. title: 'All',
  37. id: 'all',
  38. showAsAction: 'never'
  39. },
  40. {
  41. title: 'Upcoming',
  42. id: 'upcoming',
  43. showAsAction: 'never'
  44. },
  45. {
  46. title: 'My',
  47. id: 'my',
  48. showAsAction: 'never'
  49. },
  50. {
  51. title: 'Participant',
  52. id: 'participant',
  53. showAsAction: 'never'
  54. },
  55. ],
  56. fab: {
  57. collapsedId: 'share',
  58. collapsedIcon: require('../../../img/navicon_add.png'),
  59. backgroundColor: '#31363c'
  60. }
  61. };
  62. constructor(props) {
  63. super(props);
  64. this._onRowPress = this._onRowPress.bind(this);
  65. this._renderRow = this._renderRow.bind(this);
  66. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  67. this.onPress = [];
  68. this.counter = 0;
  69. const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
  70. this.state = {
  71. dataSource: ds.cloneWithRows(this._genRows({}))
  72. }
  73. }
  74. onNavigatorEvent(event) {
  75. console.log('ListScreen', 'onNavigatorEvent ', event.id);
  76. if (event.id === 'didAppear' && __STRESS_TEST__) {
  77. setTimeout(() => {
  78. this.onPress[Math.floor((Math.random() * 4))]();
  79. console.log('count', this.counter++);
  80. }, 750);
  81. }
  82. }
  83. render() {
  84. return (
  85. <ListView
  86. dataSource={this.state.dataSource}
  87. renderRow={(rowData, sectionID, rowID) => this._renderRow(rowData, sectionID, rowID)}/>
  88. );
  89. }
  90. _renderRow(rowData, sectionID, rowID) {
  91. console.log('ListScreen', '_renderRow ', rowID);
  92. const rowHash = Math.abs(hashCode(rowData.rowTitle));
  93. const sharedIconId = "image" + rowID;
  94. const sharedTitleId = "title" + rowID;
  95. const onPress = () => this._onRowPress({
  96. rowData,
  97. sharedIconId,
  98. sharedTitleId
  99. });
  100. if (['0', '1', '2', '3'].includes(rowID)) {
  101. this.onPress.push(onPress);
  102. }
  103. return (
  104. <TouchableWithoutFeedback
  105. style={styles.row}
  106. onPress={onPress}
  107. >
  108. <View style={[styles.sharedRowContent, heroStyles.primaryLight]}>
  109. {this._renderRowIcon({rowData, sharedIconId})}
  110. {this._renderRowContent({rowData, sharedTitleId, rowHash})}
  111. </View>
  112. </TouchableWithoutFeedback>
  113. );
  114. }
  115. _renderRowIcon({rowData, sharedIconId}) {
  116. return (
  117. <View>
  118. <SharedElementTransition
  119. key={sharedIconId}
  120. sharedElementId={sharedIconId}
  121. style={styles.imageContainer}
  122. >
  123. <Image
  124. source={rowData.icon}
  125. style={styles.heroIcon}
  126. fadeDuration={0}
  127. />
  128. </SharedElementTransition>
  129. </View>
  130. );
  131. }
  132. _renderRowContent({rowData, sharedTitleId, rowHash}) {
  133. return (
  134. <View style={styles.itemContentContainer}>
  135. <SharedElementTransition sharedElementId={sharedTitleId}>
  136. <Text style={styles.itemTitle}>{rowData.title}</Text>
  137. </SharedElementTransition>
  138. <Text style={styles.text}>
  139. {rowData.rowTitle + ' - ' + LOREM_IPSUM.substr(0, rowHash % 301 + 10)}
  140. </Text>
  141. </View>
  142. );
  143. }
  144. _onRowPress(data) {
  145. const {rowData, sharedIconId, sharedTitleId} = data;
  146. this.props.navigator.push({
  147. screen: 'example.HeroScreen',
  148. sharedElements: [
  149. data.sharedElementId,
  150. data.sharedElementId + 'container'
  151. ],
  152. animated: false,
  153. overrideBackPress: true,
  154. passProps: {
  155. sharedIconId: sharedIconId,
  156. sharedTitleId: sharedTitleId,
  157. ...rowData
  158. }
  159. });
  160. }
  161. _genRows() {
  162. const dataBlob = [];
  163. for (let ii = 0; ii < 100; ii++) {
  164. dataBlob.push({
  165. rowTitle: 'Row ' + ii + ' ',
  166. icon: heroes[ii % 5].icon,
  167. title: heroes[ii % 5].title,
  168. primaryColor: heroes[ii % 5].primaryColor,
  169. titleColor: heroes[ii % 5].titleColor
  170. });
  171. }
  172. return dataBlob;
  173. }
  174. _renderSeparator(sectionID, rowID) {
  175. return (
  176. <View
  177. key={`${sectionID}-${rowID}`}
  178. style={{
  179. height: 1,
  180. backgroundColor: rowID % 2 == 0 ? '#3B5998' : '#CCCCCC'
  181. }}
  182. />
  183. );
  184. }
  185. }
  186. const styles = StyleSheet.create({
  187. row: {
  188. padding: 5,
  189. height: 110,
  190. flexDirection: 'row',
  191. justifyContent: 'center',
  192. borderWidth: 1,
  193. },
  194. sharedRowContent: {
  195. flexDirection: 'row',
  196. },
  197. imageContainer: {
  198. justifyContent: 'center'
  199. },
  200. heroIcon: {
  201. width: 100,
  202. height: 100,
  203. resizeMode: 'contain'
  204. },
  205. itemContentContainer: {
  206. flex: 1,
  207. paddingLeft: 5,
  208. flexDirection: 'column',
  209. alignItems: 'flex-start'
  210. },
  211. itemTitle: {
  212. fontSize: 19,
  213. ...heroStyles.textDark
  214. },
  215. text: {
  216. ...heroStyles.textDark
  217. }
  218. });