javascript - 如何在 React-Native 中选择 ListView 的一项?

标签 javascript listview react-native

我是 React-Native 的新手。我想使用 ListView 选择一项。当我第一次按下item时,调用了ListView的renderRow(),但是终究还是不行!这个bug怎么解决?我的问题在哪里?

我在here写了一个demo

最佳答案

我最初设置了空数据源,然后将其设置为使用 componentDidMount 中的数据变量进行克隆。然后,在 onPressRow 方法中,我对数据状态变量的副本进行了必要的更改,然后通过 setState 方法将其设置回数据。不确定您的问题是什么,但现在可以使用了。

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

class ListViewDemo extends Component {

  constructor(props) {
    super(props);

    var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.state = {
      data: this._genRow(),
      dataSource: ds,
    }
  }

  componentDidMount() {
    this.setState({
      dataSource: this.state.dataSource.cloneWithRows(this.state.data)
    });
  }

  _genRow(){
    var datas = [];
    for (var i = 0; i < 5; i++) {
      datas.push({
        row: i,
        isSelect: false,
      });
    }
    console.log('datas ' + JSON.stringify(datas));
    return datas;
  }

  render() {
    return (
      <ListView
        dataSource = {this.state.dataSource}
        renderRow = {this._renderRow.bind(this)}
        renderHeader = {() => <View style={{height: 10, backgroundColor:     '#f5f5f5'}} />}
        onEndReached = {() => console.log('')}
        renderSeparator = {(sectionID, rowID) =>
          <View
            style={styles.style_separator}
            key={`${sectionID} - ${rowID}`}
          />}
      />
    );
  }

  _renderRow(rowData: string, sectionID: number, rowID: number) {
    console.log('render row ...');
    return (
      <TouchableHighlight onPress={this._onPressRow.bind(this.rowID, rowData)}>
        <View style={styles.style_row_view}>
          <Text style={styles.style_text}>{rowData.row}</Text>
          <Text style={styles.style_text}>{rowData.isSelect ? 'true' : 'false'}                   </Text>
            </View>
          </TouchableHighlight>
        );
      }

  _onPressRow(rowID, rowData) {

    rowData.isSelect = !rowData.isSelect;
    var dataClone = this.state.data;
    dataClone[rowID] = rowData;
    this.setState({
      data: dataClone
    });
    console.log(this.state.data);
  }
}

const styles = StyleSheet.create({
  style_row_view: {
    flex: 1,
    flexDirection: 'row',
    height: 57,
    backgroundColor: '#FFFFFF',
  },
  style_text: {
    flex: 1,
    marginLeft: 12,
    fontSize: 16,
    color: '#333333',
    alignSelf: 'center',
  },

});

AppRegistry.registerComponent('ListViewDemo', () => ListViewDemo);

关于javascript - 如何在 React-Native 中选择 ListView 的一项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38171253/

相关文章:

react-native - 如何将多个 ReactActivity 与单独的 js 包一起使用?

javascript - 排毒 - 在基本的 iOS RN 应用程序上努力达到 "build the Detox version of the binary, and run the tests"

javascript - 如何从自由格式文本中提取日期时间?

javascript - html 页面加载消息

android - 我的 ListView 不工作

Android SDK 未构建

javascript - ng-model 不更新选择值

java - 是否有用于在服务器端以编程方式翻译文本的 API?

java - onCreate() 被无限调用

android - Recyclerview GridLayoutManager - 如何更改项目的顺序?显示彼此相邻的两个列表