MainPage.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. *
  5. * @format
  6. * @flow
  7. */
  8. import React, { Component } from "react";
  9. import {
  10. Platform,
  11. StyleSheet,
  12. Text,
  13. View,
  14. Image,
  15. FlatList,
  16. TouchableOpacity,
  17. ImageBackground,
  18. TextInput,
  19. Button,
  20. ViewPagerAndroid,
  21. DeviceEventEmitter,
  22. SectionList
  23. } from "react-native";
  24. import Swiper from "react-native-swiper";
  25. import AndroidUtil from "../../util/AndroidUtil";
  26. import BasePage from "../BasePage";
  27. import Header from "./Header";
  28. import MySwiper from "./Swiper";
  29. import Dimensions from '../utils/dimensions';
  30. import TopicTitle from './TopicTitle';
  31. import ScrollRow from "./ScrollRow";
  32. // import console from 'console';
  33. export default class MainPage extends BasePage {
  34. constructor(props) {
  35. super(props);
  36. this.state = {
  37. grade: '大学',
  38. text: '小伙子'
  39. }
  40. }
  41. render() {
  42. return (
  43. <View style={{ flex: 1}}>
  44. {/* 尝试切换为sectionList 分组显示 */}
  45. <SectionList
  46. sections={sectionData}
  47. renderItem={({ item }) => {
  48. return this.loadFlatItem(item);
  49. }}
  50. keyExtractor={(item, index) => item.key.toString()}
  51. ListHeaderComponent={() => this.headerorfooterComponent()}
  52. renderSectionHeader={this.sectionTitle}
  53. />
  54. <View style={styles.callUpGrade}>
  55. <View style={styles.gradeItem}>
  56. <Text style={styles.gradeText}>小学</Text>
  57. </View>
  58. <View style={styles.gradeItemMid}>
  59. <Text style={styles.gradeText}>学前</Text>
  60. </View>
  61. <View style={styles.gradeItem}>
  62. <Text style={styles.gradeText}>中学</Text>
  63. </View>
  64. </View>
  65. </View>
  66. );
  67. }
  68. // 头部组件
  69. headerorfooterComponent() {
  70. return (
  71. <View>
  72. <Header
  73. uri="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1295208965,3056573814&amp;fm=26&amp;gp=0.jpg"
  74. width={400}
  75. height={150}
  76. username="卡通笨笨熊"
  77. flowerNumber="123234"
  78. onPress={() => this.toNextPage("MainActivity")}
  79. />
  80. {/* 搜索区域 */}
  81. <View style={styles.searchSection}>
  82. <TextInput
  83. style={styles.searchBox}
  84. onChangeText={(text) => this.setState({ text })}
  85. value={this.state.text}
  86. />
  87. <TouchableOpacity onPress={() => alert('press')}>
  88. <View
  89. style={styles.rightBtn}
  90. >
  91. <Image
  92. source={{ uri: 'https://facebook.github.io/react-native/docs/assets/favicon.png' }}
  93. style={styles.rightBtnIcon}
  94. />
  95. <Text
  96. style={styles.rightBtnText}
  97. >
  98. {this.state.grade}
  99. </Text>
  100. </View>
  101. </TouchableOpacity>
  102. </View>
  103. </View>
  104. );
  105. }
  106. // 渲染轮播图
  107. getSwiperElement() {
  108. console.log('进入swiper')
  109. return (
  110. <View
  111. style={{
  112. height: Dimensions.getDp(163),
  113. flex: 1,
  114. justifyContent: "center",
  115. alignItems: "center",
  116. flexDirection: "row",
  117. // backgroundColor: 'red'
  118. }}
  119. >
  120. <View
  121. style={{
  122. // width: Dimensions.width,
  123. width: Dimensions.getWidth(358),
  124. height: Dimensions.getDp(153),
  125. justifyContent: "center",
  126. overflow: 'hidden'
  127. }}
  128. >
  129. <MySwiper
  130. autoplay={true}
  131. loop={true}
  132. ifShowMiddle={true}
  133. />
  134. </View>
  135. </View>
  136. );
  137. }
  138. // 渲染课程表
  139. getScheduleElement(data) {
  140. let arr = [];
  141. let index = 0;
  142. for (let item of data.item.icon_item) {
  143. arr.push(
  144. <TouchableOpacity
  145. style={{ width: 172, height: 86, marginRight: 12 }}
  146. onPress={() => {
  147. alert(Dimensions.width.toString());
  148. }}
  149. key={index}
  150. activeOpacity={1}
  151. >
  152. <Image
  153. source={{
  154. uri: item.icon
  155. }}
  156. key={index}
  157. style={{
  158. flex: 1,
  159. justifyContent: "center",
  160. alignItems: "center",
  161. borderRadius: 10
  162. }}
  163. />
  164. </TouchableOpacity>
  165. );
  166. if (index < data.item.icon_item.length - 1) {
  167. arr.push(
  168. <View
  169. key={item.name}
  170. style={{
  171. flex: 0.1
  172. }}
  173. />
  174. );
  175. }
  176. index++;
  177. }
  178. return (
  179. <View
  180. style={{
  181. flex: 1,
  182. flexDirection: "column",
  183. height: 160,
  184. // backgroundColor: 'blue'
  185. }}
  186. >
  187. <TopicTitle title={'课程表:第一周'} ifTubeShow={true} />
  188. <View
  189. style={{
  190. flex: 2,
  191. flexDirection: "row",
  192. // backgroundColor: 'green',
  193. alignItems: 'center',
  194. paddingLeft: 10
  195. }}
  196. >
  197. {arr}
  198. </View>
  199. </View>
  200. );
  201. }
  202. // 渲染每一个块儿
  203. loadFlatItem(data) {
  204. console.log(data)
  205. console.log(data.typea)
  206. switch (data.typea) {
  207. // 轮播图
  208. case 1:
  209. console.log(1)
  210. return this.getSwiperElement();
  211. break;
  212. // 课程表
  213. case 2:
  214. console.log(2)
  215. return this.getScheduleElement(data);
  216. break;
  217. // 大图主题
  218. case 3:
  219. console.log(3)
  220. return (
  221. <View style={{
  222. flex: 1,
  223. height: 225,
  224. }}>
  225. <TopicTitle title={'五月主题:五月主题,去看看不同的世界'} summary={'每天30分钟'} ifTubeShow={true} />
  226. <ScrollRow itemWidth={352} itemHeight={153} />
  227. </View>
  228. );
  229. break;
  230. // 热门课程
  231. case 4:
  232. return (
  233. <View style={{
  234. flex: 1,
  235. height: 220,
  236. }}>
  237. <TopicTitle title={'热门广场'} ifTubeShow={true} />
  238. <ScrollRow itemWidth={106} itemHeight={150} />
  239. </View>
  240. )
  241. break;
  242. case 5:
  243. return (
  244. <View style={{
  245. flex: 1,
  246. height: 240,
  247. }}>
  248. <TopicTitle title={'生活好榜样'} summary={'死数据不是动态渲染'} />
  249. <ScrollRow itemWidth={106} itemHeight={150} />
  250. </View>
  251. )
  252. break;
  253. default:
  254. break;
  255. }
  256. }
  257. // 渲染每一个section的title
  258. sectionTitle = (info) => {
  259. console.log(info)
  260. switch (info.section.title) {
  261. case 'operation':
  262. console.log('进入运营区头部', info.section.title)
  263. return (
  264. <View
  265. style={{
  266. width: Dimensions.width,
  267. height: 10,
  268. backgroundColor: '#f0f1f5'
  269. }}
  270. />
  271. );
  272. break;
  273. case 'courses':
  274. return <TopicTitle title={'全部课程'} tubeColor={'#7d9fff'} background={'#f0f1f5'} ifTubeShow={true} />
  275. break;
  276. default:
  277. break;
  278. }
  279. }
  280. }
  281. const styles = StyleSheet.create({
  282. title_text: {
  283. justifyContent: "center",
  284. alignItems: "center",
  285. color: "red",
  286. fontSize: 30,
  287. textAlign: "center",
  288. marginTop: 30,
  289. marginBottom: 50
  290. },
  291. /* 搜索区域 */
  292. searchSection: {
  293. flex: 1,
  294. width: Dimensions.width,
  295. height: 60,
  296. flexDirection: 'row',
  297. alignItems: 'center',
  298. backgroundColor: '#f0f1f5',
  299. justifyContent: 'space-around',
  300. position: 'relative',
  301. },
  302. searchBox: {
  303. width: 232,
  304. height: 40,
  305. backgroundColor: '#fff',
  306. borderRadius: 20
  307. },
  308. rightBtn: {
  309. width: 113,
  310. height: 40,
  311. backgroundColor: '#fff',
  312. borderRadius: 20,
  313. alignItems: 'center',
  314. justifyContent: 'center',
  315. justifyContent: 'space-around',
  316. flexDirection: 'row',
  317. paddingLeft: 20,
  318. paddingRight: 10,
  319. },
  320. rightBtnIcon: {
  321. width: 14,
  322. height: 14
  323. },
  324. rightBtnText: {
  325. color: '#151515',
  326. fontSize: 18,
  327. },
  328. callUpGrade: {
  329. width: 113,
  330. height: 120,
  331. borderRadius: 20,
  332. position: 'absolute',
  333. right: 9,
  334. top: 125,
  335. zIndex:999,
  336. elevation: 999,
  337. backgroundColor: '#fff',
  338. flexDirection: 'column'
  339. },
  340. gradeItem:{
  341. zIndex:999,
  342. flex:1,
  343. alignItems: 'center',
  344. justifyContent: 'center',
  345. },
  346. gradeItemMid:{
  347. zIndex:999,
  348. flex:1,
  349. alignItems: 'center',
  350. justifyContent: 'center',
  351. borderTopWidth: 1,
  352. borderBottomWidth: 1,
  353. borderColor : '#f0f1f5'
  354. },
  355. gradeText: {
  356. color: '#151515',
  357. fontSize: 18,
  358. }
  359. });
  360. const sectionData = [
  361. {
  362. title: 'swiper',
  363. data: [{
  364. //轮播
  365. key: 111,
  366. typea: 1,
  367. text: "第一种类型"
  368. },
  369. {
  370. //只有一个标题下面两个图片
  371. key: 222,
  372. typea: 2,
  373. item: {
  374. item_type: 1,
  375. title: "课程表:第一周",
  376. icon_item: [
  377. {
  378. icon:
  379. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  380. name: "第一张图片文字"
  381. },
  382. {
  383. icon:
  384. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  385. name: "第二张图片文字"
  386. }
  387. ]
  388. }
  389. },]
  390. },
  391. {
  392. title: 'operation',
  393. data: [{
  394. //一个标题,大长图
  395. key: 444,
  396. typea: 3,
  397. title: "五月主题:五一启程,去看看不同世界",
  398. text: "每天30分钟,玩学两不误",
  399. icon_item: [
  400. {
  401. icon:
  402. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  403. name: "第一张图片文字"
  404. },
  405. {
  406. icon:
  407. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  408. name: "第二张图片文字"
  409. },
  410. {
  411. icon:
  412. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  413. name: "第三张图片文字"
  414. },
  415. {
  416. icon:
  417. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  418. name: "第四张图片文字"
  419. },
  420. {
  421. icon:
  422. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  423. name: "第五张图片文字"
  424. }
  425. ]
  426. },
  427. {
  428. key: 555,
  429. typea: 4,
  430. title: "热门课程",
  431. text: "text",
  432. mask: false,
  433. icon_item: [
  434. {
  435. icon:
  436. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  437. name: "第一张图片文字"
  438. },
  439. {
  440. icon:
  441. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  442. name: "第二张图片文字"
  443. },
  444. {
  445. icon:
  446. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  447. name: "第三张图片文字"
  448. },
  449. {
  450. icon:
  451. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  452. name: "第四张图片文字"
  453. },
  454. {
  455. icon:
  456. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  457. name: "第五张图片文字"
  458. }
  459. ]
  460. },]
  461. },
  462. {
  463. title: 'courses',
  464. data: [{
  465. //一个标题,大长图
  466. key: 444,
  467. typea: 5,
  468. title: "五月主题:五一启程,去看看不同世界",
  469. text: "每天30分钟,玩学两不误",
  470. icon_item: [
  471. {
  472. icon:
  473. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  474. name: "第一张图片文字"
  475. },
  476. {
  477. icon:
  478. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  479. name: "第二张图片文字"
  480. },
  481. {
  482. icon:
  483. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  484. name: "第三张图片文字"
  485. },
  486. {
  487. icon:
  488. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  489. name: "第四张图片文字"
  490. },
  491. {
  492. icon:
  493. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  494. name: "第五张图片文字"
  495. }
  496. ]
  497. },
  498. {
  499. key: 555,
  500. typea: 5,
  501. title: "热门课程",
  502. text: "text",
  503. mask: false,
  504. icon_item: [
  505. {
  506. icon:
  507. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  508. name: "第一张图片文字"
  509. },
  510. {
  511. icon:
  512. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  513. name: "第二张图片文字"
  514. },
  515. {
  516. icon:
  517. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  518. name: "第三张图片文字"
  519. },
  520. {
  521. icon:
  522. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  523. name: "第四张图片文字"
  524. },
  525. {
  526. icon:
  527. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556277324856&di=dc1548a0c5ba10481af922e174912937&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F05%2F12%2F82c4568a90055adcf8fbb896f0841c69.jpg",
  528. name: "第五张图片文字"
  529. }
  530. ]
  531. },]
  532. }
  533. ]