From 3ebcf18c317bf2b3987de53c0f8a706917c84b6e Mon Sep 17 00:00:00 2001 From: horizon0514 Date: Thu, 10 Dec 2015 23:12:24 +0800 Subject: [PATCH 1/6] =?UTF-8?q?list=20view=20=E6=9C=89bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataWarehouse.js | 4 +- MainScreen.js | 167 +++++++++++++++++---------------- index.ios.js | 2 +- ios/RNZhihuDaily/AppDelegate.m | 2 +- 4 files changed, 93 insertions(+), 82 deletions(-) diff --git a/DataWarehouse.js b/DataWarehouse.js index c01aa66..9d28d4d 100644 --- a/DataWarehouse.js +++ b/DataWarehouse.js @@ -1,9 +1,11 @@ 'use strict'; var API_COVER_URL = 'http://news-at.zhihu.com/api/4/start-image/1080*1776'; var API_NEWS_URL = 'http://news-at.zhihu.com/api/4/news/latest'; +var API_NEWS_BEFORE = 'http://news.at.zhihu.com/api/4/news/before/' const DataWarehouse = { cover: API_COVER_URL, - news: API_NEWS_URL + news: API_NEWS_URL, + before: API_NEWS_BEFORE } module.exports = DataWarehouse; \ No newline at end of file diff --git a/MainScreen.js b/MainScreen.js index 3af88e9..20ec393 100644 --- a/MainScreen.js +++ b/MainScreen.js @@ -18,101 +18,71 @@ var { ActivityIndicatorIOS, } = React; -let requestUrl = DataWarehouse.news; -// class MainScreen extends React.Component{ -// constructor(){//ES6 风格 -// super(); -// this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); -// return { -// dataSource: this.ds.cloneWithRows([]), -// }; -// } -// componentDidMount(){ -// //this._fetchData(); -// this.setState({dataSource:this.ds.cloneWithRows(["Row 1", "Row 2", "Row 3", "Row 4"])}); -// } - -// _fetchData(){ -// fetch(requestUrl) -// .then(response => response.json()) -// .then(response => { -// console.log(response); -// this.setState({ -// dataSource: this.ds.cloneWithRows(response.stories) -// }); - -// } -// );//setState之后会自动调用render函数 -// } -// renderRow(rowData, sectionID, rowID) { -// console.log(rowData); -// return ( -// -// -// -// -// -// {rowData.title} -// -// -// -// - -// -// - -// ); -// } - -// render() { -// console.log('render listview'); -// return ( -// -// -// -// ); -// } -// }; class MainScreen extends Component { constructor(props) { super(props); + this.state = { - isLoading : true - } - - this.ds = new ListView.DataSource( - {rowHasChanged: (r1, r2) => r1.id !== r2.id}); - this.state = { - dataSource: this.ds.cloneWithRows([{images:[]}]) + currentDay: 0, + dataBlob: {}, + sectionId: [], + dataSource: new ListView.DataSource({ + rowHasChanged: (r1,r2)=>r1 !== r2, + sectionHeaderHasChanged: (s1,s2)=>s1!==s2 + }), + loaded: false }; } componentDidMount(){ - this._fetchData(); + this._fetchData(this.state.currentDay); } - _fetchData(){ - fetch(requestUrl) + + _fetchData(date){ + var url = ''; + if(date===0){//第一次请求数据 + url = DataWarehouse.news; + } else { + url += DataWarehouse.before+date; + } + var tempDataBlob = this.state.dataBlob; + var sectionId = this.state.sectionId; + var dataBlob = this.state.dataBlob; + + fetch(url) .then(response => response.json()) .then(response => { - console.log(response); + var date = response.date; + var newDataBlob = {}; + var newSectionId = sectionId.slice(); + newSectionId.push(date); + + dataBlob[date] = response.stories; + newDataBlob = JSON.parse(JSON.stringify(dataBlob)); + + + console.log(newSectionId); this.setState({ - dataSource: this.ds.cloneWithRows(response.stories), - isLoading: false + currentDay: date, + dataBlob: newDataBlob, + sectionId: newSectionId, + dataSource: this.state.dataSource.cloneWithRowsAndSections(this.state.dataBlob,this.state.sectionId,null), + loaded: true }); - } - );//setState之后会自动调用render函数 + }) + + + } - + renderRow(rowData, sectionID, rowID) { - console.log(rowData); + //console.log(rowData); return ( @@ -130,20 +100,48 @@ class MainScreen extends Component { ); } - + renderSectionHeader(sectionData,sectionID){ + console.log(sectionID); + return ( + + {sectionID} + + ); + } + renderFooter(){ + return ( + + + ); + } + onEndReached(){ + //listview到达底部,需要继续fetchData + //console.log(this.state.currentDay); + this._fetchData(this.state.currentDay); + + } render() { + var spinner =