From 63f881311e378dda47c6af949bc658070b6039dd Mon Sep 17 00:00:00 2001 From: horizon0514 Date: Sat, 12 Dec 2015 15:34:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0viewPager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainScreen.js | 117 +++++++++++++++++++++------------ Slider.js | 69 +++++++++++++++++++ ios/RNZhihuDaily/AppDelegate.m | 2 +- package.json | 3 +- 4 files changed, 147 insertions(+), 44 deletions(-) create mode 100644 Slider.js diff --git a/MainScreen.js b/MainScreen.js index f192273..c65e82a 100644 --- a/MainScreen.js +++ b/MainScreen.js @@ -6,8 +6,9 @@ var React = require('react-native'); var DataWarehouse = require('./DataWarehouse'); -var NavigationBar = require('react-native-navbar'); var StoryView = require('./Story'); +var Slider = require('./Slider'); + var { StyleSheet, ListView, @@ -33,7 +34,10 @@ class MainScreen extends Component { currentDay: 0, dataBlob: {}, sectionId: [], - dataSource: new ListView.DataSource({ + sliderDataSource: null, + dataSource: new ListView.DataSource({//DataSource 接受这四个参数,如果数据格式不是默认的,那么需要手动实现getRowData,getSectionHeaderData两个函数 + // getRowData: this.getRowData, + // getSectionHeaderData: this.getSectionHeaderData, rowHasChanged: (r1,r2)=>r1 !== r2, sectionHeaderHasChanged: (s1,s2)=>s1!==s2 }), @@ -54,7 +58,15 @@ class MainScreen extends Component { } else { url += DataWarehouse.before+date; } - var tempDataBlob = this.state.dataBlob; + /* + * 之前的dataBlob,dataBlob数据结构为 + { + '20150101':{},// + '201412231': {} + ... + } + 这里向dataBlob中添加新数据必须返回新的对象,否则dataSource无法判断是否更新了,也就不会渲染 + */ var sectionId = this.state.sectionId; var dataBlob = this.state.dataBlob; @@ -62,6 +74,10 @@ class MainScreen extends Component { .then(response => response.json()) .then(response => { var date = response.date; + //检查是否有topstories + if(response.top_stories){ + this.state.sliderDataSource = response.top_stories; + } var newDataBlob = {}; var newSectionId = sectionId.slice(); newSectionId.push(date); @@ -71,9 +87,10 @@ class MainScreen extends Component { this.setState({ currentDay: date, + //sliderDataSource: response.top_stories||null, dataBlob: newDataBlob, sectionId: newSectionId, - dataSource: this.state.dataSource.cloneWithRowsAndSections(this.state.dataBlob,this.state.sectionId,null), + dataSource: this.state.dataSource.cloneWithRowsAndSections(newDataBlob,newSectionId,null), loaded: true }); @@ -92,20 +109,19 @@ class MainScreen extends Component { } renderRow(rowData, sectionID, rowID) { - console.log(rowData); return ( - this.rowPressed(rowData.id)} underlayColor='#dddddd'> + this.rowPressed(rowData.id)} underlayColor='#dddddd'> - - - - {rowData.title} - - - - - - + + + {rowData.title} + + + + + + + ); } @@ -139,7 +155,11 @@ class MainScreen extends Component { render() { var spinner =