React Native 平面列表水平滚动

dtcbnfnu  于 2022-11-17  发布在  React
关注(0)|答案(1)|浏览(200)

我正在创建onBoarding屏幕,我已经将每个屏幕项目创建为一个组件,并尝试使用平面列表对其进行渲染,到目前为止,一切都工作顺利,但当我滑动平面列表查看其他屏幕时,它不工作,它滑动了40%,并强制显示当前屏幕,似乎存在一些灵活的样式问题,我可以找出它,请建议。
以下是视频说明:https://youtube.com/shorts/pHbTs7ifMww
OnBoardingScreen.js

import React, { useState } from 'react';
import { Dimensions, FlatList, SafeAreaView, View, StyleSheet, Text, Image } from 'react-native';

const { width, height } = Dimensions.get('window');
const COLORS = {primary : '#ff006c', white: '#ffffff', black: '#000000'};

const slides = [
  {
    id: '1',
    image: require('../../images/OnBoardingImages/1.png'),
    title: 'You can mark time',
    description: 'Lorem ipsum is a placeholder text commonly used to demonstrate the visual',
  },

  {
    id: '2',
    image: require('../../images/OnBoardingImages/2.png'),
    title: 'You can mark time',
    description: 'Lorem ipsum is a placeholder text commonly used to demonstrate the visual',
  },

  {
    id: '3',
    image: require('../../images/OnBoardingImages/3.png'),
    title: 'You can mark time',
    description: 'Lorem ipsum is a placeholder text commonly used to demonstrate the visual',
  },
]

const Slide = ({item}) => {
  return (
    <View style={styles.slideView}>
      <Image source={item.image} style={styles.slideImage} />
      <Text style={styles.slideTitle}>{item.title}</Text>
      <Text style={styles.slideDescription}>{item.description}</Text>
    </View>
  );
}

const OnBoardingScreen = ({ navigation }) => {
  const [currentSlideIndex, setCurrentSlideIndex] = useState(0);

  const Footer = () => {
    return (
      <View style={styles.footer}>
        <View style={styles.pagination}>
          {slides.map((_, index) => (<View key={index} style={[styles.paginationItem, currentSlideIndex == index && {backgroundColor: 'grey'} ]} /> ))}
        </View>
      </View>
    );
  };

  return (
    <SafeAreaView style={styles.root}>
      <FlatList 
        data={slides}
        contentContainerStyle={{flex: 1}}
        showsHorizontalScrollIndicator={false}
        horizontal
        pagingEnabled
        renderItem={({item}) => <Slide item={item} />} />

      <Footer />
    </SafeAreaView>
  
  );
};

export default OnBoardingScreen;

const styles = StyleSheet.create({
  root: {
    flex: 1,
    backgroundColor: COLORS.white,
  },

  slideView: {
    alignItems: 'center',
  },

  slideImage: {
    height: '75%',
    width: width,
    resizeMode: 'contain',
  },

  slideTitle: {
    color: COLORS.primary,
    fontSize: '22',
    fontWeight: 'bold',
    marginVertical: 10,
    paddingHorizontal: 10,
    textAlign: 'center',
  },

  slideDescription: {
    fontSize: 18,
    paddingHorizontal: 20,
    color: 'grey',
    textAlign: 'center',
  },

  footer: {
    height: height * 0.25,
    paddingHorizontal: 20,
    justifyContent: 'space-between',
  },

  pagination: {
    flexDirection: 'row',
    justifyContent: 'center',
    marginTop: 20,
  },

  paginationItem: {
    height: 10,
    width: 10,
    backgroundColor: COLORS.primary,
    marginHorizontal: 3,
    borderRadius: 50,
  },

});

App.js

import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';

import Navigation from './src/navigation';

export default function App() {
  return (
    <View style={styles.container}>
      <Navigation />
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});
zzoitvuj

zzoitvuj1#

更新此样式

slideView: {
    alignItems: 'center',
    width: width
  },

相关问题