Select ListView 中的一行 react-native 以获取详细信息

Select a row in ListView react-native to get detail

我是 react-native 的新手。我在 https://facebook.github.io/react-native/docs/sample-application-movies.html 中看到了一个例子。 它正在构建一个电影应用程序,用于获取电影并将其显示在 ListView 中。我们如何 select 获取一行的详细数据并显示在其他视图中?请帮忙。谢谢:)

这是我目前所做的:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */

import React, {
  Component,
} from 'react';
import {
  AppRegistry,
  Image,
  ListView,
  StyleSheet,
  Text,
  View,
  Navigator,
  TouchableOpacity,
} from 'react-native';

var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';

var SCREEN_WIDTH = require('Dimensions').get('window').width;
var BaseConfig = Navigator.SceneConfigs.FloatFromRight;

var CustomLeftToRightGesture = Object.assign({}, BaseConfig.gestures.pop, {

  snapVelocity: 8,
  edgeHitWidth: SCREEN_WIDTH,
});

var CustomSceneConfig = Object.assign({}, BaseConfig, {
  springTension: 100,
  springFriction: 1,
  gestures: {
    pop: CustomLeftToRightGesture,
  }
});

var PageOne = React.createClass({
  getInitialState:function(){
    return{
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
      }),
      loaded: false,
    }
  },

  componentDidMount() {
    this.fetchData();
  },

  fetchData() {
    fetch(REQUEST_URL)
      .then((response) => response.json())
      .then((responseData) => {
        this.setState({
          dataSource: this.state.dataSource.cloneWithRows(responseData.movies),
          loaded: true,
        });
      })
      .done();
  },

  render() {
    if (!this.state.loaded) {
      return this.renderLoadingView();
    }

    return (
      <ListView
        dataSource={this.state.dataSource}
        renderRow={this.renderMovie}
        style={styles.listView}
      />
    );
  },

  renderLoadingView() {
    return (
      <View style={styles.container}>
        <Text>
          Loading movies...
        </Text>
      </View>
    );
  },

  renderMovie(movie) {
    title1 = movie.title;
    year1 = movie.year;

    return (
    <TouchableOpacity onPress={this._handlePressList}>
      <View style={styles.container}>
        <Image
          source={{uri: movie.posters.thumbnail}}
          style={styles.thumbnail}
        />
        <View style={styles.rightContainer}>
          <Text style={styles.title}>{movie.title}</Text>
          <Text style={styles.year}>{movie.year}</Text>
        </View>
      </View>
    </TouchableOpacity>
    );
  },

  _handlePressList(){
    this.props.navigator.push({id: 2, title1, year1});
  },
});

var PageTwo = React.createClass({
  render(){
    return(
      <View style={styles.rightContainer}>
          <Text style={styles.title}>{title1}</Text>
          <Text style={styles.year}>{year1}</Text>
        </View>
    )
  }
})
class SampleAppMovies extends Component{
  _renderScene(route, navigator) {
    if (route.id === 1) {
      return <PageOne navigator={navigator} />
    } else if (route.id === 2) {
      return <PageTwo navigator={navigator} />
    }
  }

  _configureScene(route) {
    return CustomSceneConfig;
  }

  render() {
    return (
      <Navigator
        initialRoute={{id: 1, }}
        renderScene={this._renderScene}
        configureScene={this._configureScene} />
    );
  }
}

var styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  rightContainer: {
    flex: 1,
  },
  title: {
    fontSize: 20,
    marginBottom: 8,
    textAlign: 'center',
  },
  year: {
    textAlign: 'center',
  },
  thumbnail: {
    width: 53,
    height: 81,
  },
  listView: {
    paddingTop: 20,
    backgroundColor: '#F5FCFF',
  },
});

module.exports = SampleAppMovies;

在您的 renderRow 方法中(在您的情况下 renderMovie),只需将电影传递到 TouchableOpacityonPress 道具中,像这样:

renderMovie(movie) {
    title1 = movie.title;
    year1 = movie.year;

    return (
    <TouchableOpacity onPress={() => _handlePressList(movie)}>    // SEE HERE!!
      <View style={styles.container}>
        <Image
          source={{uri: movie.posters.thumbnail}}
          style={styles.thumbnail}
        />
        <View style={styles.rightContainer}>
          <Text style={styles.title}>{movie.title}</Text>
          <Text style={styles.year}>{movie.year}</Text>
        </View>
      </View>
    </TouchableOpacity>
    );
  },

_handlePressList(movie){
    this.props.navigator.push({id: 2, movie.title1, movie.year1});
  },

然后你可以用movie做任何你想做的事。

希望对您有所帮助。

renderMovie(movie) {
title1 = movie.title;
year1 = movie.year;

return (
<TouchableOpacity onPress={this._handlePressList.bind(this, movie)}
  <View style={styles.container}>
    <Image
      source={{uri: movie.posters.thumbnail}}
      style={styles.thumbnail}
    />
    <View style={styles.rightContainer}>
      <Text style={styles.title}>{movie.title}</Text>
      <Text style={styles.year}>{movie.year}</Text>
    </View>
  </View>
</TouchableOpacity>
);
 },

_handlePressList(movie){
this.props.navigator.push({id: 2, movie.title1, movie.year1});
},

这将解决问题