我使用 RecyclerlistView 而不是 FlatList。它非常好,但我有 2 个问题/问题。
第一个是:
如何禁用指标?
我的第二个问题。如果我滚动,我会在几个项目上获得 fps laag。因为我在 recyclerlistview 中使用 2x 5 个项目来映射和 1x FlatList 来渲染 3-4 个图像。我怎样才能提高性能?
在 productData 中有一个来自 20 的列表,但我删除了,因为这里的代码太长了
代码:
import React, { useState, useRef } from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Animated, Dimensions, Image, FlatList, } from 'react-native';
import pure from 'recompose/pure';
import Modal from 'react-native-modal';
import DoubleClick from 'react-native-double-tap';
import Indicator from './Indicator';
import { AntDesign, Feather, Ionicons } from '@expo/vector-icons';
import { RecyclerListView, DataProvider, LayoutProvider } from 'recyclerlistview';
const width = Dimensions.get('screen').width;
const stars = [
{
"key":"1",
"star": 1,
},
{
"key":"2",
"star": 2,
},
{
"key":"3",
"star": 3,
},
{
"key":"4",
"star": 4,
},
{
"key":"5",
"star": 5,
},
];
const productData = [
{
type: 'NORMAL',
item: {
id: 1,
name:"Calvin Klein Bag",
price:"29.99€",
rating:"5",
product_image: [
{
key: "1",
image:require('../../assets/images/products/bag1.png')
},
{
key: "2",
image: require('../../assets/images/products/bag2.png')
}
],
username:"Anna_maier05",
user_profil_image:require('../../assets/images/products/12.jpg'),
trousers_size: null,
shoe_size: null,
above_size: null,
colors: ["black", "white", "#2b80ff", "red", "yellow", "pink", "gold"]
}
},
];
const ProductCard = () => {
const scrollX = useRef(new Animated.Value(0)).current;
const [testModal, setTestModal] = useState(false);
const [modal, setModal] = useState(false);
const [modalID, setModalID] = useState(null);
/* RecyclerList options */
const [dataList, setDataList] = useState(new DataProvider((r1, r2) => r1 !== r2).cloneWithRows(productData));
const [dataLayoutProvider, setDataLayoutProvider] = useState(
new LayoutProvider((i) => {
return dataList.getDataForIndex(i).type;
}, (type, dim) => {
switch(type) {
case 'NORMAL':
dim.width = width,
dim.height = 650;
break;
default:
dim.width = 0;
dim.height = 0;
break;
}
})
)
const rowRenderer = (type, data) => {
const { name, price, product_image, username, user_profil_image, colors } = data.item;
return (
<View style={styles.container}>
<View style={styles.header}>
<TouchableOpacity style={styles.headerLeft}>
<Image resizeMode="contain" style={styles.profilImage} source={user_profil_image} />
<View>
<Text style={styles.username}>{username}</Text>
<Text style={styles.usernameSubtitle}>Instagram</Text>
</View>
</TouchableOpacity>
<View style={styles.headerRight}>
<TouchableOpacity style={{marginRight: 10}}>
<Ionicons name="heart-circle-outline" size={32} color="#444" />
</TouchableOpacity>
<TouchableOpacity onPress={() => setTestModal(true)}>
<Ionicons name="ios-ellipsis-vertical" size={24} color="#444" />
</TouchableOpacity>
</View>
</View>
<View>
{ /* Product Image */ }
<Animated.FlatList
data={product_image}
horizontal
pagingEnabled
showsHorizontalScrollIndicator={false}
scrollEventThrottle={20}
onScroll={Animated.event(
[{ nativeEvent: {contentOffset: { x: scrollX}}}],
{
useNativeDriver: false
}
)}
keyExtractor={item => item.key}
renderItem={({ item }) => {
return (
<DoubleClick
singleTap={() => {
console.log("single tap");
}}
doubleTap={() => {
console.log("double tap");
}}
delay={200}
>
<Image source={item.image} resizeMode="contain" style={{height: 200, width: width * 0.9,}} />
</DoubleClick>
)
}}
/>
<Indicator scrollX={scrollX} data={product_image} backgroundColor="#444" absolute={false} />
</View>
<View style={styles.productDetailContainer}>
<View style={styles.productTitleContainer}>
<Text style={styles.productName}>{name}</Text>
<View style={styles.ratingContainer}>
{
stars.map((_, i) => {
return (
<TouchableOpacity key={`stars-${i}`}>
<AntDesign name="staro" size={20} color="#C71FF7" />
</TouchableOpacity>
)
})
}
</View>
</View>
<View style={{marginVertical: 30}}>
</View>
<Text style={{fontFamily: 'montserrat-light', color: '#555', marginBottom: 6, fontSize: 13}}>In Farben erhältlich:</Text>
<View style={styles.sizeAndColorContainer}>
<View style={styles.colorContainer}>
{
colors.length > 0 ?
colors.map((hex, i) => {
return (
<TouchableOpacity key={`color-${i}`} style={[styles.colors, { backgroundColor: hex }]}>
<Text></Text>
</TouchableOpacity>
)
})
: null
}
</View>
<TouchableOpacity style={styles.sizeButton}>
<Text style={styles.sizeButtonText}>Größe auswählen</Text>
</TouchableOpacity>
</View>
<View style={styles.buttonContainer}>
<View style={styles.bottomLeftButtons}>
<TouchableOpacity>
<Feather name="info" size={24} color="#333" />
</TouchableOpacity>
<TouchableOpacity style={styles.heartLeftMargin}>
<AntDesign name="hearto" size={24} color="#333" />
</TouchableOpacity>
</View>
<TouchableOpacity style={styles.buttonColored}>
<Text style={styles.buttonColoredText}>In den Warenkorb</Text>
</TouchableOpacity>
</View>
</View>
</View>
)
};
return (
<View style={{flex: 1, marginLeft: width * 0.05, marginRight: width * 0.05}}>
<RecyclerListView
style={{flex: 1, paddingTop: 20}}
rowRenderer={rowRenderer}
dataProvider={dataList}
layoutProvider={dataLayoutProvider}
/>
</View>
)
};