React-Native上拉刷新下拉加载,点击跳转详情代码解析

本文详细介绍如何在React-Native中实现上拉刷新与下拉加载更多功能,通过具体代码示例展示了配置路由器、设置状态机及数据请求的完整过程,并解析了组件间的交互与数据传递机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

React-Native 上拉刷新下拉加载,点击跳转详情代码解析

配置路由器

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}
        />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值