具有 3 张卡片分页布局的 React-Native FlatList

标签 react-native react-native-flatlist react-native-scrollview react-native-stylesheet

this snack我试图在屏幕中央放置 3 张卡片,并带有一个水平 FlatList 并启用分页以在滚动时跳转到接下来的 3 张卡片。
但是布局在滚动后开始被破坏,并且下一张/上一张卡片的一些像素出现在 View 中。
我应该如何设置此列表的样式,使其始终在屏幕中央正好有 3 张卡片,并且滚动将跳转到带有接下来 3 张卡片的下一页?或者像 GooglePlay 商店一样,在主要 3 张卡片的左侧和右侧可以看到上一张/下一张卡片的固定像素。 (以下示例截图)

 <View style={{flex:1,justifyContent: 'center', marginLeft: 5, marginRight: 5,}}>
      <FlatList
        horizontal
        pagingEnabled
        data={data}
        keyExtractor={(item) => `ìtem-${item}`}
        renderItem={({ item }) => (
          <Card style={{width:Dimensions.get("window").width/3-5,marginRight:5}}>
            {/* some content */}
          </Card>
        )}
      />
 </View>
我不需要像 snap-carousel 这样的图书馆或者 ...
enter image description here

最佳答案

使用 Scrollview Prop snapToOffsets实现这一目标。
像google play的例子(一一)试试snack .
enter image description here
你的例子(三乘三)试试 snack .
enter image description here
如何使用 snapToOffsets?

const snapToOffsetsLikeGooglePlay = data.map((x, i) => {
    return ((i * itemWidth) + startScroll)
})

const snapToOffsetsLikeYourExample = data.map((x, i) => {
    return ((i * (itemWidth) * previewCount) + startScroll)
})

//see the example below to know 
//what is `startScroll` and `previewCount` mean? 
//and how to calculate `itemWidth`?
这里是完整的例子
import React from 'react';
import {FlatList, Text} from 'react-native';
import { View, StyleSheet, ScrollView, Dimensions } from 'react-native';

const { width } = Dimensions.get('window');
//you need to preview n items.
const previewCount = 3;
//to center items
//the screen will show `previewCount` + 1/4 firstItemWidth + 1/4 lastItemWidth 
//so for example if previewCount = 3
//itemWidth will be =>>> itemWidth = screenWidth / (3 + 1/4 + 1/4)
const itemWidth = width/(previewCount + .5);
//to center items you start from 3/4 firstItemWidth 
const startScroll = (itemWidth * 3/4);


const App = () => {

    const data = [...Array(24).keys()];
    const flatlistRef = React.useRef();

    
    React.useEffect(() => {
        if (flatlistRef.current) flatlistRef.current.scrollToOffset({
            offset:startScroll, animated: false
        });
    }, [flatlistRef]);


    const snapToOffsetsLikeGooglePlay = data.map((x, i) => {
        return ((i * itemWidth) + startScroll)
    })

    const snapToOffsets = data.map((x, i) => {
        return ((i * (itemWidth) * previewCount) + startScroll)
    })


    return (
        <FlatList
            ref={flatlistRef}
            style={styles.container}
            pagingEnabled={true}
            horizontal= {true}
            decelerationRate={0}
            snapToOffsets={snapToOffsets}
            snapToAlignment={"center"}
            data={data}
            renderItem={({item, index}) => (
                <View style={styles.view} >
                    <Text style={styles.text}>{index}</Text>
                </View>
            )}/>
    );

}



export default App;


const styles = StyleSheet.create({
    container: {

    },
    view: {
        marginTop: 100,
        backgroundColor: '#eee',
        width: itemWidth - 20, //20 is margin left and right
        margin: 10,
        height: 140,
        borderRadius: 10,
        justifyContent : 'center',
        alignItems : 'center',
    },
    text : {
        fontSize : 60,
        fontWeight : 'bold',
        color : '#aaa',
    },

});

更新:从零开始作为@Amir-Mousavi 评论
一一尝试snack
1-) 评论 useEffect。
2-) 套装 const startScroll = (itemWidth * 3/4) enter image description here
三三试snack
1-) 评论 useEffect。
2-) 套装 const startScroll = (itemWidth * 2.75) enter image description here

关于具有 3 张卡片分页布局的 React-Native FlatList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69047058/

相关文章:

react-native - 水平 FlatList 前后的间距(React Native)

react-native - React Native FlatList Pagination 不起作用 - onEndReached 没有触发

react-native - React Native - 从另一个屏幕导航后如何将 ScrollView 滚动到给定位置

reactjs - 防止 Flatlist 在内容大小更改时滚动到顶部

react-native - 添加新项目时防止 FlatList 滚动

android - 设置 admob 后,我的应用程序崩溃而没有错误

ios - 如何在家庭网络之外的设备上运行我的 react-native 应用程序?

android - React Native Android - 图像组件(网络) - 不会通过 https 加载

javascript - react native SectionList 布局

android - React Native keep 运行失败