配置路由器
import { createTabNavigator, createStackNavigator } from "react-navigation";
import New1 from "./New1";
import New2 from "./New2";
import Web from "../Web";
const Route2 = createTabNavigator({
New1: {
screen: New1,
navigationOptions: {
title: "头条"
}
},
New2: {
screen: New2,
navigationOptions: {
title: "娱乐"
}
}
});
const Route1 = createStackNavigator({
Route2: {
screen: Route2,
navigationOptions: {
header: null
}
},
Web: Web
});
export default Route1;
导入这行依赖
// An highlighted block
import RefreshListView, { RefreshState } from "react-native-refresh-list-view";
设置状态机
constructor(props) {
super(props);
this.state = {
page: 1,
dataValue: [],
refreshstate: RefreshState.Idle
};
}
进入页面则加载数据
componentDidMount() {
this.HeaderRefresh();
}
constructor(props) {
super(props);
this.state = {
page: 1,
dataValue: [],
refreshstate: RefreshState.Idle
};
}
上拉刷新
HeaderRefresh = () => {
this.setState({
page: 1,
refreshstate: RefreshState.HeaderRefreshing
});
fetch(`https://cnodejs.org/api/v1/topics?page=1&tab=share&limit=10`)
.then(response => response.json())
.then(responseJson => {
this.setState({
dataValue: responseJson.data,
refreshstate: RefreshState.Idle,
page: this.state.page + 1
});
})
.catch(error => {
this.setState({
refreshstate: RefreshState.Failure
});
});
};
下拉加载
FooterRefresh = () => {
this.setState({
refreshstate: RefreshState.FooterRefreshing
});
fetch(
`https://cnodejs.org/api/v1/topics?page=${
this.state.page
}&tab=share&limit=10`
)
.then(response => response.json())
.then(responseJson => {
this.setState({
dataValue: [...this.state.dataValue, ...responseJson.data],
refreshstate: RefreshState.Idle,
page: this.state.page + 1
});
})
.catch(error => {
this.setState({
refreshstate: RefreshState.Failure
});
});
};
下面View调用
<RefreshListView
style={{ flex: 1 }}
data={this.state.dataValue}
renderItem={({ item }) => (
<TouchableHighlight
onPress={() => {
this.props.navigation.navigate("Web", {
content: item.content
});
}}
>
<View style={{ width: "100%" }}>
<View style={{ flexDirection: "row", marginBottom: 10 }}>
<Image
style={{ width: 60, height: 60, borderRadius: 65 }}
source={{ uri: item.author.avatar_url }}
/>
<View style={{ marginLeft: 10 }}>
<Text>{item.title}</Text>
<Text style={{ marginTop: 20 }}>
{item.author.loginname}
</Text>
</View>
</View>
<View
style={{
width: "100%",
height: 2,
backgroundColor: "#f6f6f6",
marginBottom: 10
}}
/>
</View>
</TouchableHighlight>
)}
refreshState={this.state.refreshstate}
onFooterRefresh={this.FooterRefresh}
onHeaderRefresh={this.HeaderRefresh}
/>