1. /**
    2. * Created by Bfire on 2017/11/22.
    3. */
    4. 'use strict';
    5. import React, {Component} from "react";
    6. import {ActivityIndicator, Animated, FlatList, ScrollView, StyleSheet, Text, TextInput, Button, View, Alert} from "react-native";
    7. const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
    8. const REQUEST_URL = 'https://api.github.com/search/repositories?sort=stars&q=';
    9. let that;
    10. class FlatListExample extends Component {
    11. static navigationOptions = {
    12. title: 'FlatListExample',
    13. }
    14. constructor(props) {
    15. super(props);
    16. this.state = {
    17. isLoading: true,
    18. //网络请求状态
    19. error: false,
    20. errorInfo: "",
    21. dataArray: [],
    22. txt: "",
    23. };
    24. that = this;
    25. }
    26. //网络请求
    27. fetchData() {
    28. let txt = this.state.txt ? this.state.txt : 'javascript';
    29. let url = REQUEST_URL + txt;
    30. //这个是js的访问网络的方法
    31. fetch(url)
    32. .then((response) => response.json())
    33. .then((responseData) => {
    34. let data = responseData.items;
    35. let dataBlob = [];
    36. let i = 0;
    37. data.map(function (item) {
    38. dataBlob.push({
    39. key: i,
    40. value: item,
    41. })
    42. i++;
    43. });
    44. this.setState({
    45. //复制数据源
    46. dataArray: dataBlob,
    47. isLoading: false,
    48. });
    49. data = null;
    50. dataBlob = null;
    51. })
    52. .catch((error) => {
    53. this.setState({
    54. error: true,
    55. errorInfo: error
    56. })
    57. })
    58. .done();
    59. }
    60. componentDidMount() {
    61. //请求数据
    62. this.fetchData();
    63. }
    64. onButtonPress(txt){
    65. //Alert.alert('点击了查询'+that.state.txt);
    66. //请求数据
    67. that.fetchData();
    68. }
    69. //加载等待的view
    70. renderLoadingView() {
    71. return (
    72. <View style={styles.container}>
    73. <ActivityIndicator
    74. animating={true}
    75. style={[styles.gray, {height: 80}]}
    76. color='red'
    77. size="large"
    78. />
    79. </View>
    80. );
    81. }
    82. //加载失败view
    83. renderErrorView(error) {
    84. return (
    85. <View style={styles.container}>
    86. <Text>
    87. Fail: {error}
    88. </Text>
    89. </View>
    90. );
    91. }
    92. //返回itemView
    93. renderItemView({item}) {
    94. return (
    95. <View>
    96. <Text style={styles.title}>name: {item.value.name} ({item.value.stargazers_count}
    97. stars)</Text>
    98. <Text style={styles.content}>description: {item.value.description}</Text>
    99. </View>
    100. );
    101. }
    102. renderData() {
    103. return (
    104. <ScrollView >
    105. <Text >
    106. This is React native test
    107. </Text>
    108. <TextInput
    109. style={{height: 40}}
    110. placeholder="请输入你要查询的项目名称!"
    111. onChangeText={(text) => this.setState({txt:text})}
    112. />
    113. <Button
    114. onPress={this.onButtonPress}
    115. title="查询"
    116. color="#841584"
    117. />
    118. <Text >
    119. Most popular {this.state.txt} projects In github
    120. </Text>
    121. <Text >
    122. Data:
    123. </Text>
    124. <AnimatedFlatList
    125. data={this.state.dataArray}
    126. renderItem={this.renderItemView}
    127. />
    128. </ScrollView>
    129. );
    130. }
    131. render() {
    132. //第一次加载等待的view
    133. if (this.state.isLoading && !this.state.error) {
    134. return this.renderLoadingView();
    135. } else if (this.state.error) {
    136. //请求失败view
    137. return this.renderErrorView(this.state.errorInfo);
    138. }
    139. //加载数据
    140. return this.renderData();
    141. }
    142. }
    143. const styles = StyleSheet.create({
    144. container: {
    145. flex: 1,
    146. flexDirection: 'row',
    147. justifyContent: 'center',
    148. alignItems: 'center',
    149. backgroundColor: '#F5FCFF',
    150. },
    151. title: {
    152. fontSize: 15,
    153. color: 'blue',
    154. },
    155. content: {
    156. fontSize: 15,
    157. color: 'black',
    158. },
    159. textInput: {
    160. fontSize: 16,
    161. height: 40
    162. },
    163. inputLayout: {
    164. marginTop: 16,
    165. marginHorizontal: 36
    166. }
    167. });
    168. module.exports = FlatListExample;