React Native 不规则cell渲染布局
来源:互联网 发布:mac安装不了flash 编辑:程序博客网 时间:2024/06/07 15:48
如图,cell左右两边是一样的,但是底部有一个小桌子,这样就导致了cell的整体布局不一样,所以不能使用单独的renderRow来渲染布局,此时我们将数组对象重新转成 [[],[] ]这样的数组格式
/** * Created by zhuang.haipeng on 2017/10/24. */import React, {Component} from 'react';import { View, TouchableOpacity, ListView, Dimensions, Image, Text} from 'react-native';const {width, height} = Dimensions.get('window');const dataArray = [ {"title": "title1"}, {"title": "title2"}, {"title": "title3"}, {"title": "title4"}, {"title": "title5"},];export default class extends React.Component { constructor(props) { super(props); this.dataSource = new ListView.DataSource({ //判断这两行是否相同,就是是否发生变化,决定渲染哪些行组件,避免全部渲染,提高渲染效率 rowHasChanged: (oldRow, newRow) => oldRow !== newRow }); this.state = { }; } componentWillMount() { // 转换数组[[],[]]这样的格式,拼成这样的数组,到时候数据源是一个大的数组,里面包含两个小数组。这样rowData.length=1, 保证红色小桌子的只渲染一次。 let tmpListSize = Math.round(dataArray.length / 2); let tmpTranslationDataArray = new Array(tmpListSize).fill([]).map((item, index) => { let tmpRowArray = []; let rowFirstIndex = (index * 2) + 0; let rowSecondIndex = (index * 2) + 1; tmpRowArray.push(dataArray[rowFirstIndex]); if (rowSecondIndex < dataArray.length) { tmpRowArray.push(dataArray[(index * 2) + 1]); } return tmpRowArray }); this.dataSource = this.dataSource.cloneWithRows(tmpTranslationDataArray) } renderRow = (rowData, sectionID, rowID) => { return ( <View style={{ width: width, height: 180, marginTop: 0, justifyContent: 'center', alignItems: 'center', backgroundColor:'pink' }}> <View style={{ position: 'absolute', paddingLeft: 10, paddingRight: 10, bottom: 0, left: 0, width: '100%', height: 54, }}> <Image style={{width: '100%', height: 54,top:2}} source={require('../img/cellar/ic_cellar_table.png')} /> </View> <View style={{ width: width*0.93, height: 195, justifyContent: 'center', alignItems: 'center', flexDirection: 'row', }}> <TouchableOpacity style={{flex: 1, padding: 10, alignItems: 'center'}} onPress={() => {}}> <View style={{ width: 140, height: 140, backgroundColor: '#FFF0', alignItems: 'center', justifyContent: 'center' }}> <View style={{backgroundColor:'#fff',width: 110, height: 110,borderRadius: 4}}> </View> </View> <View style={{flex: 1, height: 40}}> <Text numberOfLines={2} style={{paddingTop: 2,backgroundColor:"#FFF0", paddingBottom: 2, textAlign: 'left', color: '#c9a88d', width:120}}>{rowData[0].title}</Text> </View> </TouchableOpacity> <View style={{width: 10, height: 1}}/> { rowData.length == 2 ? <TouchableOpacity style={{flex: 1, padding:10, alignItems: 'center'}} onPress={() => {}}> <View style={{ width: 140, height: 140, backgroundColor: '#FFF0', alignItems: 'center', justifyContent: 'center' }}> <View style={{backgroundColor:'#fff',width: 110, height: 110,borderRadius: 4}}> </View> </View> <View style={{flex: 1, height: 40}}> <Text numberOfLines={2} style={{paddingTop: 2, paddingBottom: 2, textAlign: 'left', color: '#c9a88d', backgroundColor:"#FFF0", width: 120}}>{rowData[1].title}</Text> </View> </TouchableOpacity> : <View style={{flex: 1, height: 1, padding: 10}}/> } </View> </View> ) }; render() { return ( <ListView enableRefresh={true} dataSource={this.dataSource} renderRow={this.renderRow} onRefresh={this.onRefresh} onLoadMore={this.onLoadMore} /> ); }};
阅读全文
0 0
- React Native 不规则cell渲染布局
- react-native 布局
- React-Native中的布局
- React Native Flex布局
- react-native布局
- react-native flex布局
- React Native布局篇
- 【ReactNative】react-native 布局
- react native 布局篇
- React Native-FlexBox布局
- React Native 布局
- React Native Flexbox布局
- React Native FlexBox布局
- React-native 布局
- React Native FlexBox布局
- React Native FlexBox布局
- react-native布局demo1
- 【ReactNative】react-native 布局
- 集合运算
- Python--matplotlib绘图可视化知识点整理
- java 调用地图接口
- 返回给定绝对路径的,简化绝对路径
- Hessian 原理分析
- React Native 不规则cell渲染布局
- 前端的痛点之与后台和产品经理的协作
- React-Native初始化环境配置及如何去运行一个helloword工程
- sqlserver 使用 service broker 代替触发器
- 如何将本地项目上传到github
- C语言实验--计算表达式
- 【1701H1】【穆晨】【171023】连续第十三天总结
- mysql处理高并发,防止库存超卖
- Spring MVC框架搭建