【问题标题】:react native flatlist 2 items per row , width not equal反应原生 flatlist 每行 2 个项目,宽度不相等
【发布时间】:2018-10-01 08:53:50
【问题描述】:

我构建了 react 本机应用程序,我试图在 flatList 中使每行具有相等宽度的 2 个项目。 我得到的是每行 2 个项目,但宽度不相等。

const itemProduct = props => {
return (
    <Button style={style.container} onPress={props.onPress}>
        <View style={style.imageContainer}>
            <LoadingImage resizeMode='contain' resizeMethod='resize' source={props.Image ? { uri: 'https://www.saramashkim.co.il/'+props.Image } : require('../../assets/images/default_no_image.png')} style={style.image} />
        </View>
        <View style={style.textContainer}>
            <Text style={style.productName}>vodka</Text>
            <View style={style.specialOffer}>
                {props.inPromotion ? <Text>special offer</Text> : null}
            </View>

            <View style={style.bottomLine}>
                <Text style={style.costText}>lorem ipos</Text><View style={style.bottomLine}>{props.discount ? <Text style={style.Price}>lorem ipos{parseFloat(Math.round(props.price_per_case * 100) / 100).toFixed(2)}</Text> : null}<Text style={style.discountPrice}>dollar{parseFloat(Math.round(props.price_per_case * ((100 - props.discount) / 100) * 100) / 100).toFixed(2)}</Text></View>
            </View>
        </View>
    </Button>
    )
}

风格

const style = StyleSheet.create({
container : {
    backgroundColor: colors.dark_red,
    paddingTop:calcSize(10),
    margin:calcSize(10),
    minWidth:(width-15)/2,

},
imageContainer:{
    flex:1,  
    alignItems:'center'
},
textContainer:{
    flex:2,
    //backgroundColor:'red',
    paddingHorizontal:calcSize(30)
},
image:{ 
    width: calcSize(175), 
    height: calcSize(175) 
},
brandName:{
    color:'#7c7c7c',
    fontFamily:'Poppins-Light',
    fontSize:calcSize(25),
    marginBottom:calcSize(15)
},
productName:{
    color: colors.black,
    fontFamily:'Poppins-Regular',
    fontSize:calcSize(20),
    width:calcSize(300)

},

平面列表

<View style={style.list}>
   <FlatList
       refreshing={this.state.refreshing}
       numColumns = {2}
       onRefresh={this.onListRefresh}
       data={products}
       keyExtractor={(o, i) => i.toString()}
       renderItem={this.renderProductListItem}
       ListFooterComponent={() => {
          if ((this.state.products.length < this.state.totalFound || this.state.loading) && !this.state.refreshing)
             return <Spinner style={style.loading} />
             return <View style={style.loading} />
          }}

          ItemSeparatorComponent={() => <View style={style.separator} />} />
   </View>
</View>

列表样式

list:{
    flex:1,
    flexDirection:'row',
    paddingHorizontal: calcSize(30),
}

照片

我想让每行 2 个等宽的项目,也放入照片中,因为您可以看到左侧的文本不等于右侧,所以这是个问题。

【问题讨论】:

  • 很难理解你的代码,但我做了我自己的例子来看看我的答案!
  • 你的答案在哪里?

标签: react-native react-native-flatlist


【解决方案1】:

使用 flexbox 和 flexbasis 和 flex grow 试试这个

<View style={{flex: 1, flexDirection: 'row', flexWrap: 'wrap', flexGrow: 0}}>
   <View style={{flexBasis: '50%', height: 50, backgroundColor: 'powderblue'}} />
   <View style={{flexBasis: '50%', height: 50, backgroundColor: 'skyblue'}} />
   <View style={{flexBasis: '50%', height: 50, backgroundColor: 'steelblue'}} />
   <View style={{flexBasis: '50%', height: 50, backgroundColor: 'blue'}} />
</View>

在我的安卓设备中测试。

现场演示is over here!试试这个

如果有任何问题评论在下面

【讨论】:

  • 宽度在哪里?
  • flexBasis 是添加宽度属性的 flexbox 方式..!给我 2 分钟,我将添加一个现场演示
  • 我对这些元素进行了硬编码,但您始终可以从 api 动态提取数据并进行相应渲染!
  • 查看现场演示
  • 我检查过,但在这里我正在寻找一种带有动态数据的平面列表的方法。它不适合我。
【解决方案2】:

现在你已经将子元素的width设置为minWidth:(width-15)/2,margin设置为margin:calcSize(10), padding 将主容器设置为 paddingHorizontal: calcSize(30),这会导致问题。

如果你想走minWidth的方式,那么你需要正确计算间距

minWidth = (screenWidth - (paddingHorizontal * 2) - (margin * 2) - (separatorWidth)) / 2 = (screenWidth - 80) / 2

由于问题中没有提到separatorWidth

  • paddingHorizontal:设置paddingHorizontal 就像同时设置paddingLeft 和paddingRight。
  • margin: 设置边距与设置marginTop、marginLeft、marginBottom 和marginRight 的效果相同。

    list:{
        flex:1,
        paddingHorizontal: calcSize(30),
    },
    container : {
        flex: 1, //<== Either add a height or a flex
        backgroundColor: 'darkred',
        paddingTop:calcSize(10),
        margin:calcSize(10),
        minWidth:(width-80)/2,
    
    },
    

更好的方法是使用 height 和 flex ,以便 width 可以相应地缩放。

这里有零食demo

【讨论】:

    猜你喜欢
    • 2017-03-18
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2018-04-17
    相关资源
    最近更新 更多