gpt4 book ai didi

listview - React Native ListView 组件自动运行 onEndReached 函数,尽管我的滚动仍然在顶部

转载 作者:行者123 更新时间:2023-12-02 19:40:01 24 4
gpt4 key购买 nike

我使用React Native ListView组件,但我不知道为什么它会自动运行onEndReached函数,尽管滚动仍然在顶部。

我希望当滚动到设备底部时,然后运行 ​​onEndReached 函数。但在下面的代码中,当我进入该页面时,它将连续运行 onEndReached 函数。

以下是我的组件

class MovieList extends React.Component {
constructor(props) {
super(props);

this.state = {
movies: [],
loaded: false,
count: 10,
start: 0,
total: 0
};

this.dataSource = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2
});

this.REQUEST_URL = 'https://api.douban.com/v2/movie/top250';

this.fetchData();

}

requestURL(
url = this.REQUEST_URL,
count = this.state.count,
start = this.state.start
) {
return (
`${url}?count=${count}&start=${start}`
);
}

fetchData() {
fetch(this.requestURL())
.then(response => response.json())
.then(responseData => {
let newStart = responseData.start + responseData.count;
this.setState({
movies: responseData.subjects,
loaded: true,
total: responseData.total,
start: newStart,
});
})
.done();
}

renderMovieList(movie, sectionId, rowId, highlightRow) {
return (
<View>
<View style={styles.item}>
<View style={styles.itemImage}>
<Image
source={{uri: movie.images.large}}
style={styles.image}
/>
</View>
<View style={styles.itemContent}>
<Text style={styles.itemHeader}>{movie.title}</Text>
<Text style={styles.itemMeta}>
{movie.original_title} ( {movie.year} )
</Text>
<Text style={styles.redText}>
{movie.rating.average}
</Text>
</View>
</View>
</View>
);
}

loadMore() {
fetch(this.requestURL())
.then(response => response.json())
.then(responseData => {
let newStart = responseData.start + responseData.count;
this.setState({
movies: [...this.state.movies, ...responseData.subjects],
start: newStart
});
})
.done();
}

onEndReached() {
console.log(
`reached start:${this.state.start}, total:${this.state.total}`
);

if (this.state.total > this.state.start) {
this.loadMore();
}
}

renderFooter() {
if (this.state.total > this.state.start) {
return (
<View
style={{
marginVertical: 20,
paddingBottom: 50,
alignSelf: 'center'
}}
>
<ActivityIndicatorIOS />
</View>
);
} else {
return (
<View
style={{
marginVertical: 20,
paddingBottom: 50,
alignSelf: 'center'
}}
>
<Text
style={{
color: 'rgba(0, 0, 0, 0.3)'
}}
>no more load :D</Text>
</View>
);
}
}

render() {
if (!this.state.loaded) {
return (
<View style={{backgroundColor: '#eae7ff', flex: 1}}>
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<ActivityIndicatorIOS
size="large"
color="#6435c9"
/>
</View>
</View>
);
}
return (
<View style={{backgroundColor: '#eae7ff', flex: 1}}>
<ListView
renderFooter={this.renderFooter.bind(this)}
pageSize={this.state.count}
onEndReached={this.onEndReached.bind(this)}
initialListSize={this.state.count}
dataSource={this.dataSource.cloneWithRows(this.state.movies)}
renderRow={this.renderMovieList.bind(this)}
/>
</View>
);
}
}

感谢您的帮助!

最佳答案

我不知道为什么,但我从其他方式得到了一些帮助。

我删除了ScrollView组件,因为有人告诉我ListView包含ScrollView,然后给ListView组件一个style={{flex:1}},它就起作用了。

关于listview - React Native ListView 组件自动运行 onEndReached 函数,尽管我的滚动仍然在顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36347396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com