RN 使用FlatList组件 一行显示 两列内容

需求:我在移动端分页请求书籍列表数据,移动端的操作方式,多数是上拉加载,下拉刷新,选择FlatList组件的原因,它可以内部处理 随着数据增加,而产生的渲染性能问题。

效果图如下:

 

代码如下

class SearchBook extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      bookReource: [
        {bookName: '龙族', index: 0},
        {bookName: '天道', index: 1},
        {bookName: '斗破苍穹', index: 2},
        {bookName: '完美世界', index: 3},
        {bookName: '紫川', index: 4},
        {bookName: '斗罗大陆', index: 5},
        {bookName: '轮回', index: 6},
        {bookName: '众生叹', index: 7},
      ],
    };
  }
  render(){
    <FlatList
          data={this.state.bookReource}
          renderItem={({item}) => this.renderBookItem(item)}
          keyExtractor={(item) => item.index.toString()}
          numColumns={2}                       //1.一行显示两列
          showsVerticalScrollIndicator={false} //2. 不显示滑动指示器
     />
  }
  renderBookItem(item) {
    return (
      <View
        key={item.index}
        style={[
          styles.oneBook,
          item.index % 2 == 0
            ? styles.oneBookMarginRight
            : styles.oneBookMarginLeft,
        ]}>
        <Image
          style={styles.bookPoster}
          source={{uri:'在线图片路径'}}
        />
        <Text style={styles.bookTitle}>{item.bookName}</Text>
      </View>
    );
  }
}



let styles = StyleSheet.create({
  //展示书籍列表
  oneBook: {
    flex: 1, //flex设置1,可以沾满一半(因为显示两列)
    height: 230,
    borderRadius: 5,
    marginBottom: 12,
    backgroundColor: '#fff',
  },
  bookPoster: {
    borderTopRightRadius: 5,
    borderTopLeftRadius: 5,
    width: '100%',
    height: 200,
  },
  bookTitle: {
    flex: 1,
    fontSize: 14,
    textAlign: 'center',
    textAlignVertical: 'center',
  },
  oneBookMarginRight: {
    marginRight: 6,
  },
  oneBookMarginLeft: {
    marginLeft: 6,
  }
})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tengyuxin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值