react native 实现动态高度Listview 和图文混排

当考虑这个动态高度和图文混排的问题,想必你已经知道如何加载ListView和分组listview,这里不再赘述...
这里简介实现的动态的高度的cell的思路
 
 
  1. 一个ListView,准好数据源
  2. 在renderRow中渲染自定义的item
  3. 在item 中布局text 和image
  4. 在text 中实现
 onLayout={this._onTextLayout.bind(this)}
 5._onTextLayout 获得文本的高度,根据这个高度改变用ref标记的相关组件,包括text,image等等
export default class Item extends Component{


    _onTextLayout(event){

        let descHeight= event.nativeEvent.layout.height;
       totalHeight=descHeight+30>60?descHeight+45:60

        this.refs.item.setNativeProps({
            style:{
                width:Dimensions.get('window').width,height:totalHeight,backgroundColor:"white"
            }
        });
        this.refs.nextIcon.setNativeProps({
            style:{
                width:28,height:28,position:'absolute',left:Dimensions.get('window').width-35,top:totalHeight/2-12,resizeMode:Image.resizeMode.contain
            }
        });
    }

    render(){

        var row =this.props.row;
        return(
           <TouchableOpacity style={[styles.direction]} {...this.props}>
              <View ref="item" style={styles.item}>
                  <Text style={styles.title} >{row.title}</Text>
                  <Text style={styles.detailTitle} onLayout={this._onTextLayout.bind(this)}  >>{row.detail}</Text>
                  <Image ref="nextIcon" source={require('../../image/next@2x.png')} style={styles.arrow}/>
              </View>
           </TouchableOpacity>
        );
    }
}

const styles= StyleSheet.create({


    direction:{
       flexDirection:'column',borderTopWidth:1,borderTopColor:'#cccccc',},item:{
        width:Dimensions.get('window').width,height:60,backgroundColor:"white"
    },title: {
          top:10,fontSize:14,left:14,color:'#363636',backgroundColor:'transparent',detailTitle: {
        top:16,fontSize:12,color:'#999999',width:Dimensions.get('window').width-60,arrow:{
        width:28,top:18,resizeMode:Image.resizeMode.contain
    }
});

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...