Yok*_*hen 5 react-native react-native-flatlist
我正在尝试实现以下目标,而除最后一个以外的所有目标都是来自数组的普通图像。
当前,这是FlatList的代码:
<FlatList
data={images}
numColumns={3}
// ListFooterComponent={
// <View style={[StyleSheet.actionUploadedPictureFrame , {height: PIC_HEIGHT, width: PIC_WIDTH}]}>
// <Image source={require('../../../images/add-icon.png')} />
// </View>}
renderItem={ ({item, index}) => index == images.length -1 ?
<View style={{flexDirection: 'row'}}>
<Image style={[StyleSheet.actionUploadedPictureFrame , {height: PIC_HEIGHT, width: PIC_WIDTH}]} source={{uri: item.url}} />
<View style={[StyleSheet.actionUploadedPictureFrame , {height: PIC_HEIGHT, width: PIC_WIDTH}]}>
<Image source={require('../../../images/add-icon.png')} />
</View>
</View>
: <Image style={[StyleSheet.actionUploadedPictureFrame , {height: PIC_HEIGHT, width: PIC_WIDTH}]} source={{uri: item.url}} /> }
keyExtractor={(item, index) => index.toString()}
/>
Run Code Online (Sandbox Code Playgroud)
长话短说,它将数组中的图片呈现为3列的表格,并检查列表中的最后一张图片,并且除了呈现图片之外,还呈现此加号。
但是,如果列表中的元素数为3的倍数,则肯定会出现某种错误,因为加号很可能不在屏幕上。如果我使用ListFooterComponent,它将以全新的方式呈现它。
有谁知道解决这个问题的有效方法?
Mil*_*ore 16
由于我认为我不够清楚,我将尝试使用以下示例代码正确解释我的想法:
<FlatList
data={[...images, { plusImage: true }]}
numColumns={3}
renderItem={({ item }) => {
if (item.plusImage) {
return (
<View
style={[
StyleSheet.actionUploadedPictureFrame,
{ height: PIC_HEIGHT, width: PIC_WIDTH }
]}
>
<Image source={require("../../../images/add-icon.png")} />
</View>
);
}
return (
<Image
style={[
StyleSheet.actionUploadedPictureFrame,
{ height: PIC_HEIGHT, width: PIC_WIDTH }
]}
source={{ uri: item.url }}
/>
);
}}
keyExtractor={(item, index) => index.toString()}
/>;
Run Code Online (Sandbox Code Playgroud)
我不知道直接在dataprop中连接这样的数据数组是否是最佳实践,但是您始终可以选择在渲染方法中更早地声明它。让我知道这是否符合您的要求。
我也遇到了同样的情况,并在检查文档时找到了一个好方法,FlatList因此我推荐的最佳方法是使用ListFooterComponent您可以使用ListFooterComponentStyle Click here for more details传递该元素的样式对象。
| 归档时间: |
|
| 查看次数: |
1201 次 |
| 最近记录: |