react-native-navigation的迁移库

Masonry.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React from 'react';
  2. import {StyleSheet, View, Text, PixelRatio, FlatList, Image, TouchableHighlight} from 'react-native';
  3. import {Navigation, SharedElementTransition} from 'react-native-navigation';
  4. import images from './images';
  5. const ROW_HEIGHT = 650;
  6. const COLS = 2;
  7. class Masonry extends React.Component {
  8. onAssetPress = (image, key) => {
  9. this.props.navigator.push({
  10. screen: 'example.Transitions.SharedElementTransitions.Masonry.Item',
  11. sharedElements: [key],
  12. passProps: {
  13. image,
  14. sharedImageId: key,
  15. },
  16. });
  17. };
  18. renderAsset = (asset, row, column, index) => {
  19. const key = `row_${row}_column_${column}_asset_${index}`;
  20. return (
  21. <TouchableHighlight
  22. key={key}
  23. onPress={() => {
  24. this.onAssetPress(asset.source, key);
  25. }}
  26. style={[styles.assetContainer, {flex: asset.weight}]}
  27. >
  28. <View style={{flex: 1}}>
  29. <SharedElementTransition
  30. style={{flex: 1}}
  31. sharedElementId={key}
  32. >
  33. <Image
  34. source={asset.source}
  35. resizeMode={'cover'}
  36. style={styles.asset}
  37. />
  38. </SharedElementTransition>
  39. </View>
  40. </TouchableHighlight>
  41. );
  42. };
  43. renderItem = ({item, index}) => {
  44. return (
  45. <View style={[styles.item, {height: ROW_HEIGHT}]}>
  46. {[...new Array(COLS)].map((column, columnIndex) => (
  47. <View
  48. key={`row_${index}_column_${columnIndex}`}
  49. style={{flex: 1}}
  50. >
  51. {item.images[columnIndex].map((asset, assetIndex) => this.renderAsset(asset, index, columnIndex, assetIndex))}
  52. </View>
  53. ))}
  54. </View>
  55. );
  56. };
  57. render() {
  58. return (
  59. <View style={styles.container}>
  60. <FlatList
  61. data={images}
  62. renderItem={this.renderItem}
  63. getItemLayout={(layout, index) => ({length: ROW_HEIGHT, offset: ROW_HEIGHT * index, index})}
  64. />
  65. </View>
  66. );
  67. }
  68. }
  69. const styles = StyleSheet.create({
  70. container: {
  71. flex: 1,
  72. backgroundColor: '#ffffff',
  73. },
  74. item: {
  75. flex: 1,
  76. flexDirection: 'row',
  77. },
  78. assetContainer: {
  79. margin: 5,
  80. borderRadius: 6,
  81. borderWidth: StyleSheet.hairlineWidth,
  82. },
  83. asset: {
  84. flex: 1,
  85. borderRadius: 6,
  86. },
  87. });
  88. export default Masonry;