React native ListView初识

前端之家收集整理的这篇文章主要介绍了React native ListView初识前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ListView的一个小栗子:
例子内容部分参考地址:http://www.jb51.cc/article/p-cptgfkam-re.html
工程目录机构:

新建一个DramaComponent.js自定义一个公共组件

  1. import React,{Component} from 'react';
  2. import {
  3. View,Text,ListView,StyleSheet,Image,TouchableOpacity,} from 'react-native';
  4. // 导入Dimensions库
  5. var Dimensions = require('Dimensions');
  6.  
  7. export default class DramaComponent extends Component{
  8. constructor(props){
  9. super(props);
  10. this.state = {
  11. movies:new ListView.DataSource({
  12. rowHasChanged:(r1,r2) => r1!=r2,}),}
  13. }
  14. //组件的生命周期函数
  15. componentDidMount(){
  16. var datas = [{
  17. name:'原来是美男啊',//影片名称
  18. title:'第1集中字',//标题
  19. actor:'xx',//演员
  20. pic:'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1940296251,2372471969&fm=116&gp=0.jpg',//图片地址
  21. url:'http://www.y3600.com/hanju/2016/907.html',//详情链接
  22. },{
  23. name:'原来是美男啊',//影片名称
  24. title:'第2集中字',//演员
  25. pic:'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1646612829,79869058&fm=116&gp=0.jpg',//影片名称
  26. title:'第3集中字',//影片名称
  27. title:'第4集中字',//演员
  28. pic:'http://img.y3600.com/d/file/p/2016/08/24/b216e94526fbf2d49f40dc5aaa1897a4.jpg',//影片名称
  29. title:'第5集中字',//详情链接
  30. }
  31. ];
  32. this.setState({
  33. movies:this.state.movies.cloneWithRows(datas),});
  34. }
  35.  
  36. //渲染ListView item view
  37. _renderMovieView(movie){
  38. return(
  39. <View style={styles.row} key={movie.url}>
  40. <TouchableOpacity onPress={this._onItemPress.bind(this,movie)} activeOpacity={0.8} >
  41. <View>
  42. <Image source={{uri:movie.pic}} style={styles.thumbnail}>
  43. <Text style={styles.title}>{movie.title}</Text>
  44. </Image>
  45. <Text numberOfLines={1} style={styles.name}>{movie.name}</Text>
  46. <Text numberOfLines={1} style={styles.actor}>{movie.actor}</Text>
  47. </View>
  48. </TouchableOpacity>
  49. </View>);
  50. }
  51. //ListView 拉到底部调用
  52. _onEndReached(){
  53.  
  54. }
  55.  
  56. //item 点击事件
  57. _onItemPress(movie){
  58. console.log(movie);
  59. }
  60.  
  61. render(){
  62. return(
  63. <ListView
  64. dataSource = {this.state.movies}
  65. renderRow = {this._renderMovieView.bind(this)}
  66. style = {styles.listview}
  67. initialListSize = {10}
  68. pageSize = {10}
  69. onEndReachedThreshold = {5}
  70. onEndReached = {this._onEndReached.bind(this)}
  71. enableEmptySections = {true}
  72. contentContainerStyle = {styles.grid}
  73. />
  74. );
  75. }
  76. }
  77.  
  78. //样式
  79. const WIN_WIDTH = Dimensions.get('window').width;
  80. var width = WIN_WIDTH/3;
  81. var styles = StyleSheet.create({
  82. grid:{
  83. justifyContent: 'flex-start',flexDirection: 'row',flexWrap: 'wrap',marginTop:10
  84. },row:{
  85. height:140,width:width,flexDirection:'column',justifyContent:'center',alignItems:'center',paddingTop:10,marginTop:5,},thumbnail:{
  86. flex:1,width:width-20,height:80,justifyContent:'flex-end',resizeMode: Image.resizeMode.strech,title:{
  87. fontSize:10,textAlign:'center',color:'white',backgroundColor:'#27272790',name:{
  88. fontSize:12,color:'black',marginTop:8,marginBottom:5,actor:{
  89. fontSize:10,color:'#707070',listview:{
  90. backgroundColor:'#f5fcff',});

调用

  1. import DramaComponent from './js/component/DramaComponent'
  2. <View style={styles.container}>
  3. <DramaComponent />
  4. </View>

效果图:

猜你在找的React相关文章