1.海淘半小时热门   基本功能和首页相似

GDHt.js

  1. /**
  2. * 海淘折扣
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. TouchableOpacity,
  10. Image,
  11. ListView,
  12. Dimensions,
  13. ActivityIndicator,
  14. Modal, // 模态
  15. AsyncStorage, // 缓存数据库(数据持久化)
  16. } from 'react-native';
  17.  
  18. // 引入 下拉刷新组件
  19. import {PullList} from 'react-native-pull';
  20. // 导航器
  21. import CustomerComponents, {
  22. Navigator
  23. } from 'react-native-deprecated-custom-components';
  24.  
  25. // 获取屏幕宽高
  26. const {width, height} = Dimensions.get('window');
  27.  
  28. // 引入自定义导航栏组件
  29. import CommunalNavBar from '../main/GDCommunalNavBar';
  30. // 引入 近半小时热门组件
  31. import USHalfHourHot from './GDUSHalfHourHot';
  32. // 引入 搜索页面组件
  33. import Search from '../main/GDSearch';
  34. // 引入 cell
  35. import CommunalHotCell from '../main/GDCommunalHotCell';
  36. // 引入 详情页 组件
  37. import CommunalDetail from '../main/GDCommunalDetail';
  38. // 引入 空白页组件
  39. import NoDataView from '../main/GDNoDataView';
  40.  
  41. // 引入 HTTP封装组件
  42. import HTTPBase from '../http/HTTPBase';
  43.  
  44. export default class GDHome extends Component {
  45.  
  46. // 构造
  47. constructor(props) {
  48. super(props);
  49. // 初始状态
  50. this.state = {
  51. dataSource: new ListView.DataSource({rowHasChanged:(r1, r2) => r1 !== r2}), // 数据源 优化
  52. loaded: false, // 用于判断是否显示空白页
  53. isModal: false, // 用于判断模态的可见性
  54. };
  55. // 全局定义一个空数组用于存储列表数据
  56. this.data = [];
  57. // 绑定
  58. this.loadData = this.loadData.bind(this);
  59. this.loadMore = this.loadMore.bind(this);
  60. }
  61.  
  62. // 加载最新数据网络请求
  63. loadData(resolve) {
  64.  
  65. let params = {
  66. "count" : 10,
  67. "country" : "us"
  68. };
  69.  
  70. HTTPBase.get('https://guangdiu.com/api/getlist.php', params)
  71. .then((responseData) => {
  72.  
  73. // 清空数组(刷新时)
  74. this.data = [];
  75.  
  76. // 拼接数据
  77. this.data = this.data.concat(responseData.data);
  78.  
  79. // 重新渲染
  80. this.setState({
  81. dataSource: this.state.dataSource.cloneWithRows(this.data),
  82. loaded:true,
  83. });
  84.  
  85. // 关闭刷新动画
  86. if (resolve !== undefined){
  87. setTimeout(() => {
  88. resolve();
  89. }, 1000);
  90. }
  91.  
  92. // 存储数组中最后一个元素的id
  93. let uslastID = responseData.data[responseData.data.length - 1].id;
  94. AsyncStorage.setItem('uslastID', uslastID.toString()); // 只能存储字符或字符串
  95.  
  96. })
  97. .catch((error) => {
  98.  
  99. })
  100. }
  101.  
  102. // 加载更多数据的网络请求
  103. loadMoreData(value) {
  104.  
  105. let params = {
  106. "count" : 10,
  107. "country" : "us",
  108. "sinceid" : value
  109. };
  110.  
  111. HTTPBase.get('https://guangdiu.com/api/getlist.php', params)
  112. .then((responseData) => {
  113.  
  114. // 拼接数据
  115. this.data = this.data.concat(responseData.data);
  116.  
  117. // 重新渲染
  118. this.setState({
  119. dataSource: this.state.dataSource.cloneWithRows(this.data),
  120. loaded:true,
  121. });
  122.  
  123. // 存储数组中最后一个元素的id
  124. let uslastID = responseData.data[responseData.data.length - 1].id;
  125. AsyncStorage.setItem('uslastID', uslastID.toString()); // 只能存储字符或字符串
  126.  
  127. })
  128. .catch((error) => {
  129.  
  130. })
  131. }
  132.  
  133. // 加载更多数据操作
  134. loadMore() {
  135. // 读取存储的id
  136. AsyncStorage.getItem('uslastID')
  137. .then((value) => {
  138. // 数据加载操作
  139. this.loadMoreData(value);
  140. })
  141. }
  142.  
  143. // 模态到近半小时热门
  144. pushToHalfHourHot() {
  145. this.setState({
  146. isModal: true
  147. })
  148. }
  149.  
  150. // 跳转到搜索页面
  151. pushToSearch() {
  152. this.props.navigator.push({
  153. component: Search,
  154. })
  155. }
  156.  
  157. // 安卓模态销毁模态
  158. onRequestClose() {
  159. this.setState({
  160. isModal: false
  161. })
  162. }
  163.  
  164. // 关闭模态
  165. closeModal(data) {
  166. this.setState({
  167. isModal:data
  168. })
  169. }
  170.  
  171. // 返回左边按钮
  172. renderLeftItem() {
  173. // 将组件返回出去
  174. return(
  175. <TouchableOpacity
  176. onPress={() => {this.pushToHalfHourHot()}}
  177. >
  178. <Image source={{uri:'hot_icon_20x20'}} style={styles.navbarLeftItemStyle} />
  179. </TouchableOpacity>
  180. );
  181. }
  182.  
  183. // 返回中间按钮
  184. renderTitleItem() {
  185. return(
  186. <TouchableOpacity>
  187. <Image source={{uri:'navtitle_home_down_66x20'}} style={styles.navbarTitleItemStyle} />
  188. </TouchableOpacity>
  189. );
  190. }
  191.  
  192. // 返回右边按钮
  193. renderRightItem() {
  194. return(
  195. <TouchableOpacity
  196. // 跳转搜索页面
  197. onPress={() => {this.pushToSearch()}}
  198. >
  199. <Image source={{uri:'search_icon_20x20'}} style={styles.navbarRightItemStyle} />
  200. </TouchableOpacity>
  201. );
  202. }
  203.  
  204. // ListView尾部
  205. renderFooter() {
  206. return (
  207. <View style={{height: 100}}>
  208. <ActivityIndicator />
  209. </View>
  210. );
  211. }
  212.  
  213. // 根据网络状态决定是否渲染 listView
  214. renderListView() {
  215. if(this.state.loaded === false) {
  216. // 显示空白页
  217. return(
  218. <NoDataView />
  219. );
  220. }else{
  221. return(
  222. <PullList // 将ListView 改为 PullList
  223. // 下拉刷新
  224. onPullRelease={(resolve) => this.loadData(resolve)}
  225. // 数据源 通过判断dataSource是否有变化,来判断是否要重新渲染
  226. dataSource={this.state.dataSource}
  227. renderRow={this.renderRow.bind(this)}
  228. // 隐藏水平线
  229. showsHorizontalScrollIndicator={false}
  230. style={styles.listViewStyle}
  231. initialListSize={5}
  232. // 返回 listView 头部
  233. renderHeader={this.renderHeader}
  234. // 上拉加载更多
  235. onEndReached={this.loadMore}
  236. onEndReachedThreshold={60}
  237. renderFooter={this.renderFooter}
  238. />
  239. );
  240. }
  241. }
  242.  
  243. // 通过id 跳转详情页
  244. pushToDetail(value) {
  245. this.props.navigator.push({
  246. component:CommunalDetail,
  247. params: {
  248. url: 'https://guangdiu.com/api/showdetail.php' + '?' + 'id=' + value
  249. }
  250. })
  251. }
  252.  
  253. // 返回每一行cell的样式
  254. renderRow(rowData) {
  255. // 使用cell组件
  256. return(
  257. <TouchableOpacity
  258. // 给每一个cell添加点击事件
  259. onPress={() => this.pushToDetail(rowData.id)}
  260. >
  261. <CommunalHotCell
  262. image={rowData.image}
  263. title={rowData.title}
  264. />
  265. </TouchableOpacity>
  266. );
  267. }
  268.  
  269. // 生命周期 组件渲染完成 已经出现在dom文档里
  270. componentDidMount() {
  271. // 请求数据
  272. this.loadData();
  273. }
  274.  
  275. render() {
  276. return (
  277. <View style={styles.container}>
  278. {/* 初始化模态 */}
  279. <Modal
  280. animationType='slide' // 动画 底部弹窗
  281. transparent={false} // 透明度
  282. visible={this.state.isModal} // 可见性
  283. onRequestClose={() => this.onRequestClose()} // 销毁
  284. >
  285. <Navigator
  286. initialRoute={{
  287. name:'halfHourHot',
  288. component:USHalfHourHot
  289. }}
  290.  
  291. renderScene={(route, navigator) => {
  292. let Component = route.component;
  293. return <Component
  294. removeModal={(data) => this.closeModal(data)}
  295. {...route.params}
  296. navigator={navigator} />
  297. }} />
  298. </Modal>
  299.  
  300. {/* 导航栏样式 */}
  301. <CommunalNavBar
  302. leftItem = {() => this.renderLeftItem()}
  303. titleItem = {() => this.renderTitleItem()}
  304. rightItem = {() => this.renderRightItem()}
  305. />
  306.  
  307. {/* 根据网络状态决定是否渲染 listView */}
  308. {this.renderListView()}
  309. </View>
  310. );
  311. }
  312. }
  313.  
  314. const styles = StyleSheet.create({
  315. container: {
  316. flex: 1,
  317. alignItems: 'center',
  318. },
  319. navbarLeftItemStyle: {
  320. width:20,
  321. height:20,
  322. marginLeft:15,
  323. },
  324. navbarTitleItemStyle: {
  325. width:66,
  326. height:20,
  327. },
  328. navbarRightItemStyle: {
  329. width:20,
  330. height:20,
  331. marginRight:15,
  332. },
  333.  
  334. listViewStyle: {
  335. width:width,
  336. },
  337. });

GDUSHalfHourHot.js

  1. /**
  2. * 近半小时热门(US)
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. TouchableOpacity,
  10. Image,
  11. ListView,
  12. Dimensions,
  13. DeviceEventEmitter,
  14. } from 'react-native';
  15.  
  16. // 获取屏幕宽高
  17. const {width, height} = Dimensions.get('window');
  18.  
  19. // 引入自定义导航栏组件
  20. import CommunalNavBar from '../main/GDCommunalNavBar';
  21. // 引入 cell
  22. import CommunalHotCell from '../main/GDCommunalHotCell';
  23. // 引入 详情页 组件
  24. import CommunalDetail from '../main/GDCommunalDetail';
  25. // 引入 空白页组件
  26. import NoDataView from '../main/GDNoDataView';
  27. // 引入 下拉刷新组件
  28. import {PullList} from 'react-native-pull';
  29.  
  30. // 引入 HTTP封装组件
  31. import HTTPBase from '../http/HTTPBase';
  32.  
  33. export default class GDUSHalfHourHot extends Component {
  34.  
  35. // 构造
  36. constructor(props) {
  37. super(props);
  38. // 初始状态
  39. this.state = {
  40. dataSource: new ListView.DataSource({rowHasChanged:(r1, r2) => r1 !== r2}), // 数据源 优化
  41. loaded: false, // 用于判断是否显示空白页
  42. };
  43. // 绑定
  44. this.fetchData = this.fetchData.bind(this);
  45. }
  46.  
  47. // 提供参数出去,便于外部调用
  48. static defaultProps = {
  49. removeModal:{}
  50. }
  51.  
  52. // 网络请求
  53. fetchData(resolve) {
  54.  
  55. let params = {
  56. "c" : "us"
  57. }
  58.  
  59. HTTPBase.get('http://guangdiu.com/api/gethots.php', params)
  60. .then((responseData) => { // 处理数据
  61. // 修改dataSource的值
  62. this.setState({
  63. dataSource: this.state.dataSource.cloneWithRows(responseData.data),
  64. loaded:true,
  65. });
  66. // 关闭下拉刷新动画
  67. if (resolve !== undefined){
  68. // 使用定时器 延时关闭动画
  69. setTimeout(() => {
  70. resolve(); // 关闭动画
  71. }, 1000);
  72. }
  73. })
  74. .catch((error) => {
  75.  
  76. })
  77. }
  78.  
  79. popToHome(data) {
  80. // 向外部传值
  81. this.props.removeModal(data);
  82. }
  83.  
  84. // 返回中间按钮
  85. renderTitleItem() {
  86. return(
  87. <Text style={styles.navbarTitleItemStyle}>近半小时热门</Text>
  88. );
  89. }
  90.  
  91. // 返回右边按钮
  92. renderRightItem() {
  93. return(
  94. <TouchableOpacity
  95. onPress={() => {this.popToHome(false)}}
  96. >
  97. <Text style={styles.navbarRightItemStyle}>关闭</Text>
  98. </TouchableOpacity>
  99. );
  100. }
  101.  
  102. // 根据网络状态决定是否渲染 listView
  103. renderListView() {
  104. if(this.state.loaded === false) {
  105. // 显示空白页
  106. return(
  107. <NoDataView />
  108. );
  109. }else{
  110. return(
  111. <PullList // 将ListView 改为 PullList
  112. // 下拉刷新
  113. onPullRelease={(resolve) => this.fetchData(resolve)}
  114. // 数据源 通过判断dataSource是否有变化,来判断是否要重新渲染
  115. dataSource={this.state.dataSource}
  116. renderRow={this.renderRow.bind(this)}
  117. // 隐藏水平线
  118. showsHorizontalScrollIndicator={false}
  119. style={styles.listViewStyle}
  120. initialListSize={5}
  121. // 返回 listView 头部
  122. renderHeader={this.renderHeader}
  123. />
  124. );
  125. }
  126. }
  127.  
  128. // 返回 listView 头部
  129. renderHeader() {
  130. return(
  131. <View style={styles.headerPromptStyle}>
  132. <Text>根据每条折扣的点击进行统计,每5分钟更新一次</Text>
  133. </View>
  134. );
  135. }
  136.  
  137. // 通过id 跳转详情页
  138. pushToDetail(value) {
  139. this.props.navigator.push({
  140. component:CommunalDetail,
  141. params: {
  142. url: 'http://guangdiu.com/api/showdetail.php' + '?' + 'id=' + value
  143. }
  144. })
  145. }
  146.  
  147. // 返回每一行cell的样式
  148. renderRow(rowData) {
  149. // 使用cell组件
  150. return(
  151. <TouchableOpacity
  152. // 给每一个cell添加点击事件
  153. onPress={() => this.pushToDetail(rowData.id)}
  154. >
  155. <CommunalHotCell
  156. image={rowData.image}
  157. title={rowData.title}
  158. />
  159. </TouchableOpacity>
  160. );
  161. }
  162.  
  163. componentWillMount() {
  164. // 向GDMain.js 发送通知 隐藏tabBar
  165. DeviceEventEmitter.emit('isHiddenTabBar', true);
  166. }
  167.  
  168. componentWillUnmount() {
  169. // 向GDMain.js 发送通知 显示tabBar
  170. DeviceEventEmitter.emit('isHiddenTabBar', false);
  171. }
  172.  
  173. // 生命周期 组件渲染完成 已经出现在dom文档里
  174. componentDidMount() {
  175. // 请求数据
  176. this.fetchData();
  177. }
  178.  
  179. render() {
  180. return (
  181. <View style={styles.container}>
  182. {/* 导航栏样式 */}
  183. <CommunalNavBar
  184. titleItem = {() => this.renderTitleItem()}
  185. rightItem = {() => this.renderRightItem()}
  186. />
  187.  
  188. {/* 根据网络状态决定是否渲染 listView */}
  189. {this.renderListView()}
  190. </View>
  191. );
  192. }
  193. }
  194.  
  195. const styles = StyleSheet.create({
  196. container: {
  197. flex:1,
  198. alignItems: 'center',
  199. },
  200.  
  201. navbarTitleItemStyle: {
  202. fontSize:17,
  203. color:'black',
  204. marginLeft:50
  205. },
  206. navbarRightItemStyle: {
  207. fontSize:17,
  208. color:'rgba(123,178,114,1.0)',
  209. marginRight:15
  210. },
  211.  
  212. headerPromptStyle: {
  213. height:44,
  214. width:width,
  215. backgroundColor:'rgba(239,239,239,0.5)',
  216. justifyContent:'center',
  217. alignItems:'center'
  218. },
  219.  
  220. listViewStyle: {
  221. width:width,
  222. },
  223. });

效果图:

  

2.获取最新数据个数功能 (角标)

GDHome.js

  1. /**
  2. * 首页
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. TouchableOpacity,
  10. Image,
  11. ListView,
  12. Dimensions,
  13. ActivityIndicator,
  14. Modal, // 模态
  15. AsyncStorage, // 缓存数据库(数据持久化)
  16. } from 'react-native';
  17.  
  18. // 引入 下拉刷新组件
  19. import {PullList} from 'react-native-pull';
  20. // 导航器
  21. import CustomerComponents, {
  22. Navigator
  23. } from 'react-native-deprecated-custom-components';
  24.  
  25. // 获取屏幕宽高
  26. const {width, height} = Dimensions.get('window');
  27.  
  28. // 引入自定义导航栏组件
  29. import CommunalNavBar from '../main/GDCommunalNavBar';
  30. // 引入 近半小时热门组件
  31. import HalfHourHot from './GDHalfHourHot';
  32. // 引入 搜索页面组件
  33. import Search from '../main/GDSearch';
  34. // 引入 cell
  35. import CommunalHotCell from '../main/GDCommunalHotCell';
  36. // 引入 详情页 组件
  37. import CommunalDetail from '../main/GDCommunalDetail';
  38. // 引入 空白页组件
  39. import NoDataView from '../main/GDNoDataView';
  40.  
  41. // 引入 HTTP封装组件
  42. import HTTPBase from '../http/HTTPBase';
  43.  
  44. export default class GDHome extends Component {
  45.  
  46. // 构造
  47. constructor(props) {
  48. super(props);
  49. // 初始状态
  50. this.state = {
  51. dataSource: new ListView.DataSource({rowHasChanged:(r1, r2) => r1 !== r2}), // 数据源 优化
  52. loaded: false, // 用于判断是否显示空白页
  53. isModal: false, // 用于判断模态的可见性
  54. };
  55. // 全局定义一个空数组用于存储列表数据
  56. this.data = [];
  57. // 绑定
  58. this.loadData = this.loadData.bind(this);
  59. this.loadMore = this.loadMore.bind(this);
  60. }
  61.  
  62. // 加载最新数据网络请求
  63. loadData(resolve) {
  64.  
  65. let params = {"count" : 10 };
  66.  
  67. HTTPBase.get('https://guangdiu.com/api/getlist.php', params)
  68. .then((responseData) => {
  69.  
  70. // 情况数组(刷新时)
  71. this.data = [];
  72.  
  73. // 拼接数据
  74. this.data = this.data.concat(responseData.data);
  75.  
  76. // 重新渲染
  77. this.setState({
  78. dataSource: this.state.dataSource.cloneWithRows(this.data),
  79. loaded:true,
  80. });
  81.  
  82. // 关闭刷新动画
  83. if (resolve !== undefined){
  84. setTimeout(() => {
  85. resolve();
  86. }, 1000);
  87. }
  88.  
  89. // 存储数组中最后一个元素的id
  90. let cnlastID = responseData.data[responseData.data.length - 1].id;
  91. AsyncStorage.setItem('cnlastID', cnlastID.toString()); // 只能存储字符或字符串
  92.  
  93. // 首页存储数组中第一个元素的id
  94. let cnfirstID = responseData.data[0].id;
  95. AsyncStorage.setItem('cnfirstID', cnfirstID.toString());
  96.  
  97. })
  98. .catch((error) => {
  99.  
  100. })
  101. }
  102.  
  103. // 加载更多数据的网络请求
  104. loadMoreData(value) {
  105.  
  106. let params = {
  107. "count" : 10,
  108. "sinceid" : value
  109. };
  110.  
  111. HTTPBase.get('https://guangdiu.com/api/getlist.php', params)
  112. .then((responseData) => {
  113.  
  114. // 拼接数据
  115. this.data = this.data.concat(responseData.data);
  116.  
  117. // 重新渲染
  118. this.setState({
  119. dataSource: this.state.dataSource.cloneWithRows(this.data),
  120. loaded:true,
  121. });
  122.  
  123. // 存储数组中最后一个元素的id
  124. let cnlastID = responseData.data[responseData.data.length - 1].id;
  125. AsyncStorage.setItem('cnlastID', cnlastID.toString()); // 只能存储字符或字符串
  126.  
  127. })
  128. .catch((error) => {
  129.  
  130. })
  131. }
  132.  
  133. // 加载更多数据操作
  134. loadMore() {
  135. // 读取存储的id
  136. AsyncStorage.getItem('cnlastID')
  137. .then((value) => {
  138. // 数据加载操作
  139. this.loadMoreData(value);
  140. })
  141. }
  142.  
  143. // 模态到近半小时热门
  144. pushToHalfHourHot() {
  145. this.setState({
  146. isModal: true
  147. })
  148. }
  149.  
  150. // 跳转到搜索页面
  151. pushToSearch() {
  152. this.props.navigator.push({
  153. component: Search,
  154. })
  155. }
  156.  
  157. // 安卓模态销毁模态
  158. onRequestClose() {
  159. this.setState({
  160. isModal: false
  161. })
  162. }
  163.  
  164. // 关闭模态
  165. closeModal(data) {
  166. this.setState({
  167. isModal:data
  168. })
  169. }
  170.  
  171. // 返回左边按钮
  172. renderLeftItem() {
  173. // 将组件返回出去
  174. return(
  175. <TouchableOpacity
  176. onPress={() => {this.pushToHalfHourHot()}}
  177. >
  178. <Image source={{uri:'hot_icon_20x20'}} style={styles.navbarLeftItemStyle} />
  179. </TouchableOpacity>
  180. );
  181. }
  182.  
  183. // 返回中间按钮
  184. renderTitleItem() {
  185. return(
  186. <TouchableOpacity>
  187. <Image source={{uri:'navtitle_home_down_66x20'}} style={styles.navbarTitleItemStyle} />
  188. </TouchableOpacity>
  189. );
  190. }
  191.  
  192. // 返回右边按钮
  193. renderRightItem() {
  194. return(
  195. <TouchableOpacity
  196. // 跳转搜索页面
  197. onPress={() => {this.pushToSearch()}}
  198. >
  199. <Image source={{uri:'search_icon_20x20'}} style={styles.navbarRightItemStyle} />
  200. </TouchableOpacity>
  201. );
  202. }
  203.  
  204. // ListView尾部
  205. renderFooter() {
  206. return (
  207. <View style={{height: 100}}>
  208. <ActivityIndicator />
  209. </View>
  210. );
  211. }
  212.  
  213. // 根据网络状态决定是否渲染 listView
  214. renderListView() {
  215. if(this.state.loaded === false) {
  216. // 显示空白页
  217. return(
  218. <NoDataView />
  219. );
  220. }else{
  221. return(
  222. <PullList // 将ListView 改为 PullList
  223. // 下拉刷新
  224. onPullRelease={(resolve) => this.loadData(resolve)}
  225. // 数据源 通过判断dataSource是否有变化,来判断是否要重新渲染
  226. dataSource={this.state.dataSource}
  227. renderRow={this.renderRow.bind(this)}
  228. // 隐藏水平线
  229. showsHorizontalScrollIndicator={false}
  230. style={styles.listViewStyle}
  231. initialListSize={5}
  232. // 返回 listView 头部
  233. renderHeader={this.renderHeader}
  234. // 上拉加载更多
  235. onEndReached={this.loadMore}
  236. onEndReachedThreshold={60}
  237. renderFooter={this.renderFooter}
  238. />
  239. );
  240. }
  241. }
  242.  
  243. // 通过id 跳转详情页
  244. pushToDetail(value) {
  245. this.props.navigator.push({
  246. component:CommunalDetail,
  247. params: {
  248. url: 'https://guangdiu.com/api/showdetail.php' + '?' + 'id=' + value
  249. }
  250. })
  251. }
  252.  
  253. // 返回每一行cell的样式
  254. renderRow(rowData) {
  255. // 使用cell组件
  256. return(
  257. <TouchableOpacity
  258. // 给每一个cell添加点击事件
  259. onPress={() => this.pushToDetail(rowData.id)}
  260. >
  261. <CommunalHotCell
  262. image={rowData.image}
  263. title={rowData.title}
  264. />
  265. </TouchableOpacity>
  266. );
  267. }
  268.  
  269. // 生命周期 组件渲染完成 已经出现在dom文档里
  270. componentDidMount() {
  271. // 请求数据
  272. this.loadData();
  273. }
  274.  
  275. render() {
  276. return (
  277. <View style={styles.container}>
  278. {/* 初始化模态 */}
  279. <Modal
  280. animationType='slide' // 动画 底部弹窗
  281. transparent={false} // 透明度
  282. visible={this.state.isModal} // 可见性
  283. onRequestClose={() => this.onRequestClose()} // 销毁
  284. >
  285. <Navigator
  286. initialRoute={{
  287. name:'halfHourHot',
  288. component:HalfHourHot
  289. }}
  290.  
  291. renderScene={(route, navigator) => {
  292. let Component = route.component;
  293. return <Component
  294. removeModal={(data) => this.closeModal(data)}
  295. {...route.params}
  296. navigator={navigator} />
  297. }} />
  298. </Modal>
  299.  
  300. {/* 导航栏样式 */}
  301. <CommunalNavBar
  302. leftItem = {() => this.renderLeftItem()}
  303. titleItem = {() => this.renderTitleItem()}
  304. rightItem = {() => this.renderRightItem()}
  305. />
  306.  
  307. {/* 根据网络状态决定是否渲染 listView */}
  308. {this.renderListView()}
  309. </View>
  310. );
  311. }
  312. }
  313.  
  314. const styles = StyleSheet.create({
  315. container: {
  316. flex: 1,
  317. alignItems: 'center',
  318. },
  319. navbarLeftItemStyle: {
  320. width:20,
  321. height:20,
  322. marginLeft:15,
  323. },
  324. navbarTitleItemStyle: {
  325. width:66,
  326. height:20,
  327. },
  328. navbarRightItemStyle: {
  329. width:20,
  330. height:20,
  331. marginRight:15,
  332. },
  333.  
  334. listViewStyle: {
  335. width:width,
  336. },
  337. });

GDMain.js

  1. /**
  2. * 主页面
  3. * 通过此文件连接其他文件
  4. */
  5. import React, {
  6. Component
  7. } from 'react';
  8. import {
  9. StyleSheet,
  10. Text,
  11. View,
  12. Image,
  13. Platform,
  14. DeviceEventEmitter,
  15. AsyncStorage,
  16. } from 'react-native';
  17.  
  18. // tab组件(第三方框架)
  19. import TabNavigator from 'react-native-tab-navigator';
  20. // 导航器
  21. import CustomerComponents, {
  22. Navigator
  23. } from 'react-native-deprecated-custom-components';
  24.  
  25. // 引入其他组件
  26. import Home from '../home/GDHome';
  27. import HT from '../ht/GDHt';
  28. import HourList from '../hourList/GDHourList';
  29. // 引入 HTTP封装组件
  30. import HTTPBase from '../http/HTTPBase';
  31.  
  32. export default class GD extends Component {
  33. // ES6
  34. // 构造
  35. constructor(props) {
  36. super(props);
  37. // 初始状态
  38. this.state = {
  39. selectedTab: 'home',
  40. isHiddenTabBar:false, // 是否隐藏tabbar
  41. // 状态机制 角标
  42. cnbadgeText:'',
  43. usbadgeText:'',
  44. };
  45. }
  46.  
  47. // 设置Navigator跳转动画
  48. setNavAnimationType(route) {
  49. if(route.animationType) { // 外部有值传入
  50. // 关闭返回手势
  51. let conf = route.animationType;
  52. conf.gestures = null;
  53. return conf;
  54. }else{
  55. // 默认动画
  56. return Navigator.SceneConfigs.PushFromRight;
  57. }
  58. }
  59.  
  60. // 返回TabBar的Item
  61. renderTabBarItem(title, selectedTab, image, selectedImage, component, badgeText) {
  62. return (
  63. <TabNavigator.Item
  64. selected={this.state.selectedTab === selectedTab}
  65. badgeText={badgeText == 0 ? '' : badgeText} // 角标
  66. title={title}
  67. selectedTitleStyle={{color:'black'}}
  68. renderIcon={() => <Image source={{uri:image}} style={styles.tabbarIconStyle} />}
  69. renderSelectedIcon = {() => <Image source={{uri:selectedImage}} style={styles.tabbarIconStyle} />}
  70. onPress = {() => this.setState({selectedTab: selectedTab})}>
  71. <Navigator
  72. // 设置路由
  73. initialRoute = {
  74. {
  75. name: selectedTab,
  76. component: component
  77. }
  78. }
  79.  
  80. // 设置导航动画
  81. configureScene={(route) => this.setNavAnimationType(route)}
  82.  
  83. renderScene = {
  84. (route, navigator) => {
  85. let Component = route.component;
  86. return <Component {...route.params} navigator={navigator} />
  87. }
  88. } />
  89. </TabNavigator.Item>
  90. );
  91. }
  92.  
  93. tongZhi(data) {
  94. this.setState({
  95. isHiddenTabBar:data,
  96. })
  97. }
  98.  
  99. // 使用通知,进行隐藏和显示tabBar
  100. componentDidMount() {
  101. // 注册通知
  102. this.subscription = DeviceEventEmitter.addListener('isHiddenTabBar', (data)=>{this.tongZhi(data)});
  103.  
  104. // 初始化数据的个数(角标)
  105. let cnfirstID = 0;
  106. let usfirstID = 0;
  107.  
  108. // 最新
  109. setInterval(() => {
  110. // 取出id
  111. AsyncStorage.getItem('cnfirstID')
  112. .then((value) => {
  113. cnfirstID = parseInt(value); // 强转成整数型
  114. });
  115. AsyncStorage.getItem('usfirstID')
  116. .then((value) => {
  117. usfirstID = parseInt(value);
  118. });
  119.  
  120. if (cnfirstID !== 0 && usfirstID !== 0) { // 参数不为0
  121. // 拼接参数
  122. let params = {
  123. "cnmaxid" : cnfirstID,
  124. "usmaxid" : usfirstID
  125. };
  126.  
  127. // 请求数据
  128. HTTPBase.get('http://guangdiu.com/api/getnewitemcount.php', params)
  129. .then((responseData) => {
  130.  
  131. console.log(responseData);
  132.  
  133. // 修改 状态值
  134. this.setState({
  135. cnbadgeText:responseData.cn,
  136. usbadgeText:responseData.us
  137. })
  138. })
  139. .catch((error) => {
  140.  
  141. })
  142. }
  143. },3000)
  144. }
  145.  
  146. componentWillUnmount() {
  147. // 销毁
  148. this.subscription.remove();
  149. }
  150.  
  151. render() {
  152. return (
  153. <TabNavigator
  154. tabBarStyle={this.state.isHiddenTabBar !== true ? {} : {height:0, overflow:'hidden'}}
  155. sceneStyle={this.state.isHiddenTabBar !== true ? {} : {paddingBottom:0}}
  156. >
  157. { /* 首页 */ }
  158. {this.renderTabBarItem("首页", 'home', 'tabbar_home_30x30', 'tabbar_home_selected_30x30', Home, this.state.cnbadgeText)}
  159. { /* 海淘 */ }
  160. {this.renderTabBarItem("海淘", 'ht', 'tabbar_abroad_30x30', 'tabbar_abroad_selected_30x30', HT, this.state.usbadgeText)}
  161. { /* 小时风云榜 */ }
  162. {this.renderTabBarItem("小时风云榜", 'hourlist', 'tabbar_rank_30x30', 'tabbar_rank_selected_30x30', HourList)}
  163. </TabNavigator>
  164. );
  165. }
  166. }
  167.  
  168. const styles = StyleSheet.create({
  169. container: {
  170. flex: 1,
  171. justifyContent: 'center',
  172. alignItems: 'center',
  173. backgroundColor: '#F5FCFF',
  174. },
  175. tabbarIconStyle: {
  176. width: Platform.OS === 'ios' ? 30 : 25,
  177. height: Platform.OS === 'ios' ? 30 : 25,
  178. }
  179. });

核心代码:

这里需要 cnmaxid 和 usmaxid 参数,他们分别是最新数据中第一个元素的 id,也就是我们每次 刷新 的时候都保存一下数组中的第一个元素的 id。

  1. // 首页存储数组中第一个元素的id
  2. let cnfirstID = responseData.data[0].id;
  3. AsyncStorage.setItem('cnfirstID', cnfirstID.toString());

这个功能是从程序启动的时候就开始 定时循环执行 ,也就是我们需要放到 入口文件中(Main文件)。

  1. // 使用通知,进行隐藏和显示tabBar
  2. componentDidMount() {
  3. // 注册通知
  4. this.subscription = DeviceEventEmitter.addListener('isHiddenTabBar', (data)=>{this.tongZhi(data)});
  5.  
  6. // 初始化数据的个数(角标)
  7. let cnfirstID = 0;
  8. let usfirstID = 0;
  9.  
  10. // 最新
  11. setInterval(() => {
  12. // 取出id
  13. AsyncStorage.getItem('cnfirstID')
  14. .then((value) => {
  15. cnfirstID = parseInt(value); // 强转成整数型
  16. });
  17. AsyncStorage.getItem('usfirstID')
  18. .then((value) => {
  19. usfirstID = parseInt(value);
  20. });
  21.  
  22. if (cnfirstID !== 0 && usfirstID !== 0) { // 参数不为0
  23. // 拼接参数
  24. let params = {
  25. "cnmaxid" : cnfirstID,
  26. "usmaxid" : usfirstID
  27. };
  28.  
  29. // 请求数据
  30. HTTPBase.get('http://guangdiu.com/api/getnewitemcount.php', params)
  31. .then((responseData) => {
  32.  
  33. console.log(responseData);
  34.  
  35. // 修改 状态值
  36. this.setState({
  37. cnbadgeText:responseData.cn,
  38. usbadgeText:responseData.us
  39. })
  40. })
  41. .catch((error) => {
  42.  
  43. })
  44. }
  45. },3000)
  46. }

效果图:

.

React-Native 之 GD (十二)海淘半小时热门 及 获取最新数据个数功能 (角标)的更多相关文章

  1. [RN] React Native 让 Flatlist 支持 选中多个值,并获取所选择的值

    React Native 让 Flatlist  支持  选中多个值,并获取所选择的值 实现效果如下: 实现代码: import React, {Component} from 'react'; im ...

  2. React-Native 之 GD (三)近半小时热门

    1.设置页面跳转 半小时热门组件  GDHalfHourHot.js /** * 近半小时热门 */ import React, { Component } from 'react'; import ...

  3. react native 项目使用 expo 二维码扫描失败

    今天学习react native,需使用expo在移动端进行调试. npm start 运行项目后,使用expo扫描二维码,始终没有反应.于是决定采用这个方法: 连上手机打开usb调试后,按下‘a’, ...

  4. React Native学习(十)—— 生命周期

    本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...

  5. React Native细节知识点总结<二>

    1.关于React Native导出组件的export default和export的问题: 一个文件只能有一个export default,可以有多个export export class Temp ...

  6. React文档(十二)组合vs继承

    React拥有很强大的组合模型,我们建议使用组合来替代继承来重利用组件之间的代码. 在本章节中,我们将讨论一些开发者经常触及继承的问题,并且我们该如何使用组合来解决这些问题. 组合 一些组件事先不知道 ...

  7. SpringCloud微服务实战——搭建企业级开发框架(二十二):基于MybatisPlus插件TenantLineInnerInterceptor实现多租户功能

    多租户技术的基本概念:   多租户技术(英语:multi-tenancy technology)或称多重租赁技术,是一种软件架构技术,它是在探讨与实现如何于多用户的环境下共用相同的系统或程序组件,并且 ...

  8. ADO.NET 快速入门(十二):从 SQL Server 生成 XML 数据

    本文演示如何使用2种不同的方法从 SQL Server 生成 XML.   方法1:使用了 SqlCommand 的 ExecuteXmlReader 方法获取 XmlReader,然后使用 Data ...

  9. [原创.数据可视化系列之十二]使用 nodejs通过async await建立同步数据抓取

    做数据分析和可视化工作,最重要的一点就是数据抓取工作,之前使用Java和python都做过简单的数据抓取,感觉用的很不顺手. 后来用nodejs发现非常不错,通过js就可以进行数据抓取工作,类似jqu ...

随机推荐

  1. 2019 CSP-S初赛游记

    2019-10-19 ——这个注定要被载入史册的日子 作为一名初中生,和lpy大佬一同参加提高组的比赛,而今年普及组和提高组的时间竟然不一样,于是——凌晨六点半,来到了pdyz和高中生一起坐车去. 高 ...

  2. if练习

    练习: 1.简述变量命名规范 1.变量名由字母.下划线.数字组成 2.变量名不能以数字开头 3.变量不能使用python中的关键字 4.变量不能使用中文和拼音 5.区分大小写 6.变量名要具有描述性 ...

  3. SSM框架中数据库无法连接的问题

    首先是SSM框架中所有的配置都是没有问题的,而且项目在其他人的环境上也能正常访问数据库:那么最有可能的就是数据库版本的问题导致数据库连接不上,服务器给我的报错是: 15:37:25.902 [C3P0 ...

  4. go & cron

    https://github.com/robfig/cron - 源码 cron - GoDoc 参考 go---定时任务 cron,gin 静态文件 go语言里比较好用的计划任务调度模块

  5. 剑指offer 分行从上到下打印二叉树

    题目: 从上到下按层打印二叉树,同一层的节点按照从左到右的顺序打印,每一层打印到一行. /* struct TreeNode { int val; struct TreeNode *left; str ...

  6. nginx配置反向代理,解决前端开发的跨域问题

    适用:开发和生产环境 配置如下 server { listen 10901; server_name res.pre.ices.red; #charset koi8-r; #access_log lo ...

  7. Spring基础05——Spring依赖注入的三种方式

    Spring支持3种依赖注入的方式:属性注入.构造器注入.工厂 1.属性注入 属性注入即通过setter方法注入Bean的属性或依赖的对象.使用<property>元素,使用name属性指 ...

  8. 使用modelsim直接仿真IP(FIFO)

      不通过quartus仿真,简单的仿真,就不用建立工程了,直接建立个简单的库 1.改变当前modelsim的工作路径到quartus工程的根目录下 2.新建库 3.添加必要的文件 这里第一个框,要选 ...

  9. VB中preserve的用法

    注:本文转载自:http://zhidao.baidu.com/question/161401549.html ReDim 语句用来定义或重定义原来已经用带空圆括号(没有维数下标)的 Private. ...

  10. evpp心跳机制

    client server xin good