美文网首页
ReactNative-ListView(十)

ReactNative-ListView(十)

作者: 攻克乃还_ | 来源:发表于2017-10-16 08:08 被阅读22次

此组件已过期 - 请使用FlatList或SectionList代替。

一、ListView原理

  • ListView内部是通过DataSource对象显示数据,因此使用ListView要先创建DataSource对象
  • highlightRow:高亮函数。函数需要传入两个参数,组ID,行ID

二、使用ListView步骤

1.创建数据源对象 new ListView.DataSource()
2.设置数据 cloneWithRow []
3.实现渲染每一行方法 renderRow

代码示例:

import React, {Component} from 'react'
import
{
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
    Button,
    TextInput,
    Image,
    Dimensions,
    ListView
}
    from 'react-native'

class ReactDemo extends Component {
    constructor(props){
        super(props);
        // 创建数据源
        ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

        // 设置数据
        ds = ds.cloneWithRows(['row1','row2']);
        this.state = {
            ds:ds
        }
    }

    render(){
        return (
            <ListView dataSource={this.state.ds}
                      renderRow={this._renderRow.bind(this)}
                      style={{marginTop:20,backgroundColor:'red'}}
                      renderHeader={this._renderHeader.bind(this)}
                      renderFooter={this._renderFooter.bind(this)}
                      onScroll={(e)=>{
                          console.log(e.nativeEvent);
                      }}
            />
        )
}

    _renderHeader(){
        return ( <View style={{backgroundColor:'blue',height:300}}></View>)
    }
    _renderFooter(){
        return (<View style={{backgroundColor:'yellow',height:200}}></View>)
    }
    // 渲染分割线
    _renderSeparator(){
        return (
            <View style={{height:1,backgroundColor:'#e8e8e8'}}>
            </View>
        )
    }
    // 返回每一行的外观
    _renderRow(rowData,sectionID,rowID,highlightRow){
        return (
            <TouchableOpacity>
                <View style={{
                    height:44,
                    justifyContent:'center',
                    backgroundColor:'green',
                    borderBottomWidth:1,
                    borderBottomColor:'#e8e8e8'
                }} onPress={()=>{
                highlightRow(sectionID,rowID);
                }}>
                    <Text>{rowData}</Text>
                </View>
            </TouchableOpacity>
        )
    }
}
var styles = StyleSheet.create({
})
AppRegistry.registerComponent('ReactDemo',()=>ReactDemo);

相关文章

  • ReactNative-ListView(十)

    此组件已过期 - 请使用FlatList或SectionList代替。 一、ListView原理 ListView...

  • “十·十”

    心中一直有一个不大不小的秘密,时间久了,真怕忘记了。 就留在这里吧,也不打算带走了...... 第一篇 初识 ...

  • 十数年,十数次,十数感

    有那么两部电影陪我走过春夏秋冬,陪我走完青春年少,《大话西游之月光宝盒》和《大话西游之大圣娶亲》。第二部大家总说是...

  • 魔道十幸十虐十憾

    一幸:二人相遇时正当韶好年华 二幸:魏无羡、江澄、师姐三人从小相识,感情甚笃 三幸:年龄相似、志同道合 四幸:年少...

  • 十穷十富

    十穷 第一穷,多因放荡不经营——逐渐穷; 第二穷,不惜钱财手头松——容易穷; 第三穷,朝朝睡到日头红——邋遢穷; ...

  • 十问十答

    第一问:微商发展至今,由萌芽期快速过渡到爆发期,然后进入低谷期。国家对微商的政策也在不断的出台完善。你认为,现阶段...

  • 十月(十)

    黄昏凉起来 刹那间落泪的时候 是今晚的月牙儿 触动了你柔软的内心 巷子和树已老 有点江湖的味道 不怕走错街道 就怕...

  • 十犬十美

    “咳咳,各位亲人,各位朋友,新年好!在此充满正能量的大年初一,本人特地请来了江湖号称‘一字断命’的汪大师给大家拜年...

  • 十月(十)

    近来时常想起外婆。次数很频繁,经常有穿越回童年的感觉。 无尽的宠溺、呵护,奠定了我一生的幸福基础。幼年时,满山满野...

  • 十 '逾越节' 十

    耶和华在埃及地晓谕摩西、亚伦说: “你们要以本月为正月,为一年之首。 你们吩咐以色列全会众说:本月初十日,各人要按...

网友评论

      本文标题:ReactNative-ListView(十)

      本文链接:https://www.haomeiwen.com/subject/ehzcuxtx.html