|
| 1 | +import React from 'react'; |
| 2 | +import { Image, StyleSheet, View, SafeAreaView, Animated } from 'react-native'; |
| 3 | + |
| 4 | +import { LazyPagerView } from 'react-native-pager-view'; |
| 5 | + |
| 6 | +import { LikeCount } from './component/LikeCount'; |
| 7 | +import { NavigationPanel } from './component/NavigationPanel'; |
| 8 | +import { useNavigationPanel } from './hook/useNavigationPanel'; |
| 9 | +import type { CreatePage } from './utils'; |
| 10 | + |
| 11 | +const AnimatedPagerView = Animated.createAnimatedComponent(LazyPagerView); |
| 12 | + |
| 13 | +function keyExtractor(page: CreatePage) { |
| 14 | + return `${page.key}`; |
| 15 | +} |
| 16 | + |
| 17 | +function renderItem({ item }: { item: CreatePage }) { |
| 18 | + return ( |
| 19 | + <View style={item.style}> |
| 20 | + <Image style={styles.image} source={item.imgSource} /> |
| 21 | + <LikeCount /> |
| 22 | + </View> |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +export function LazyPagerViewExample() { |
| 27 | + const { ref, ...navigationPanel } = useNavigationPanel< |
| 28 | + LazyPagerView<unknown> |
| 29 | + >(); |
| 30 | + |
| 31 | + return ( |
| 32 | + <SafeAreaView style={styles.container}> |
| 33 | + <AnimatedPagerView |
| 34 | + ref={ref} |
| 35 | + style={styles.PagerView} |
| 36 | + initialPage={0} |
| 37 | + overdrag={navigationPanel.overdragEnabled} |
| 38 | + scrollEnabled={navigationPanel.scrollEnabled} |
| 39 | + onPageScroll={navigationPanel.onPageScroll} |
| 40 | + onPageSelected={navigationPanel.onPageSelected} |
| 41 | + onPageScrollStateChanged={navigationPanel.onPageScrollStateChanged} |
| 42 | + pageMargin={10} |
| 43 | + // Lib does not support dynamically orientation change |
| 44 | + orientation="horizontal" |
| 45 | + // Lib does not support dynamically transitionStyle change |
| 46 | + transitionStyle="scroll" |
| 47 | + showPageIndicator={navigationPanel.dotsEnabled} |
| 48 | + data={navigationPanel.pages} |
| 49 | + keyExtractor={keyExtractor} |
| 50 | + renderItem={renderItem} |
| 51 | + /> |
| 52 | + <NavigationPanel {...navigationPanel} /> |
| 53 | + </SafeAreaView> |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +const styles = StyleSheet.create({ |
| 58 | + container: { |
| 59 | + flex: 1, |
| 60 | + backgroundColor: 'white', |
| 61 | + }, |
| 62 | + image: { |
| 63 | + width: 300, |
| 64 | + height: 200, |
| 65 | + padding: 20, |
| 66 | + }, |
| 67 | + PagerView: { |
| 68 | + flex: 1, |
| 69 | + }, |
| 70 | +}); |
0 commit comments