【发布时间】:2019-03-07 03:28:49
【问题描述】:
我想映射一个数组,它可能包含偶数或奇数个元素,我想在我的 react native 组件中每行显示 2 条记录,<Viewstyle={{flexDirection: 'row',height: 180,width: '83.33%',
alignSelf: 'center', justifyContent: 'center'}}></View> 请问如何通过 2 条记录映射数组,而不仅仅是一条记录
这是我迄今为止尝试过的
const goods = (
<FlatList
data={this.state.goods}
renderItem={({ item }) => (
<TouchableNativeFeedback
onPress={() =>
this.props.navigation.navigate('ProductDetail', {name: item.name,
amount: item.amount,
description: item.description,
images: item.images,
qty: item.quantity,
id: item.id}
)}>
<View style={{flexDirection: 'row',height: 180,width: '83.33%',
alignSelf: 'center', justifyContent: 'center'}}>
<TouchableNativeFeedback onPress={() =>
this.props.navigation.navigate('ProductDetail', {})}>
<View style={{flex: 1, flexDirection: 'column',justifyContent: 'space-evenly'}}>
<View style={{height: Height*(17.19/100),
width: Width*(40/100),
marginRight: 10,
borderTopRightRadius: 6,
borderTopLeftRadius: 6,
borderBottomLeftRadius: 6,
borderBottomRightRadius: 6,
backgroundColor: '#FAFAFA',
borderWidth: 0.4,
borderColor: '#DADADA',
elevation: 0}}>
<Image resizeMode="contain" style={{alignSelf: 'center', flex: 1}}
source={require('../headset.png')}/>
</View>
<View style={{flexDirection: 'column'}}>
<Text style={{fontFamily: 'mont-semi',fontSize: 12,color: '#615d5d',
paddingLeft: 10}}>
Headset
</Text>
<Text style={{fontFamily: 'mont-medium',color: '#615d5d',fontSize: 8
,paddingLeft: 10}}>
₦230,000.00
</Text>
</View>
</View></TouchableNativeFeedback>
<TouchableNativeFeedback onPress={() =>
this.props.navigation.navigate('ProductDetail', {})}>
<View style={{height:180, flexDirection: 'column', justifyContent: 'space-evenly'}}>
<View style={{height: Height*(17.19/100),
width: Width*(40/100),
marginRight: 10,
borderTopRightRadius: 6,
borderTopLeftRadius: 6,
borderBottomLeftRadius: 6,
borderBottomRightRadius: 6,
backgroundColor: '#FAFAFA',
borderWidth: 0.4,
borderColor: '#DADADA',
elevation: 0}}>
<Image resizeMode="contain" style={{alignSelf: 'center', flex: 1}}
source={require('../case_.png')}/>
</View>
<View style={{flexDirection: 'column'}}>
<Text style={{fontFamily: 'mont-semi',fontSize: 12,color: '#615d5d',
paddingLeft: 10}}>
Headset
</Text>
<Text style={{fontFamily: 'mont-medium',color: '#615d5d',fontSize: 8
,paddingLeft: 10}}>
₦230,000.00
</Text>
</View>
</View></TouchableNativeFeedback>
</View>
</TouchableNativeFeedback>
)}
keyExtractor={item => item.id}
horizontal={true}
/> );
以下是我希望如何呈现数据,每行 2 条记录,我阅读了数组缩减文档,但我怀疑它是否适用于这种情况
<View style={{flex: 1,
flexDirection: 'column'}}>
<View style={{flexDirection: 'row',height: 180,width: '83.33%',
alignSelf: 'center', justifyContent: 'center'}}>
<TouchableNativeFeedback onPress={() =>
this.props.navigation.navigate('ProductDetail', {})}>
<View style={{flex: 1, flexDirection: 'column',justifyContent: 'space-evenly'}}>
<View style={{height: Height*(17.19/100),
width: Width*(40/100),
marginRight: 10,
borderTopRightRadius: 6,
borderTopLeftRadius: 6,
borderBottomLeftRadius: 6,
borderBottomRightRadius: 6,
backgroundColor: '#FAFAFA',
borderWidth: 0.4,
borderColor: '#DADADA',
elevation: 0}}>
<Image resizeMode="contain" style={{alignSelf: 'center', flex: 1}}
source={require('../headset.png')}/>
</View>
<View style={{flexDirection: 'column'}}>
<Text style={{fontFamily: 'mont-semi',fontSize: 12,color: '#615d5d',
paddingLeft: 10}}>
Headset
</Text>
<Text style={{fontFamily: 'mont-medium',color: '#615d5d',fontSize: 8
,paddingLeft: 10}}>
₦230,000.00
</Text>
</View>
</View></TouchableNativeFeedback>
<TouchableNativeFeedback onPress={() =>
this.props.navigation.navigate('ProductDetail', {})}>
<View style={{height:180, flexDirection: 'column', justifyContent: 'space-evenly'}}>
<View style={{height: Height*(17.19/100),
width: Width*(40/100),
marginRight: 10,
borderTopRightRadius: 6,
borderTopLeftRadius: 6,
borderBottomLeftRadius: 6,
borderBottomRightRadius: 6,
backgroundColor: '#FAFAFA',
borderWidth: 0.4,
borderColor: '#DADADA',
elevation: 0}}>
<Image resizeMode="contain" style={{alignSelf: 'center', flex: 1}}
source={require('../case_.png')}/>
</View>
<View style={{flexDirection: 'column'}}>
<Text style={{fontFamily: 'mont-semi',fontSize: 12,color: '#615d5d',
paddingLeft: 10}}>
Headset
</Text>
<Text style={{fontFamily: 'mont-medium',color: '#615d5d',fontSize: 8
,paddingLeft: 10}}>
₦230,000.00
</Text>
</View>
</View></TouchableNativeFeedback>
</View>
</View>
【问题讨论】:
-
您是否尝试过按数组中的 2 行对记录进行分组,然后遍历分组数组?
-
我不熟悉react-native,但是可以用css grid吗?如果是这样,可以直观地实现效果(无需先对数组进行分组)。
标签: javascript reactjs react-native