React-Native中react-native-tab-view的使用

本文详细介绍了如何在React Native应用中使用`react-native-tab-view`组件创建一个带有懒加载和个性化选项的Tab切换界面,包括设置样式、路由管理及事件监听。

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

react-native-tab-view

安装:

npm install --save react-native-tab-view

使用:
import * as React from 'react';
import {
  Animated,
  View,
  Text,
  TouchableOpacity,
  Dimensions,
  StyleSheet,
} from 'react-native';
import {TabView, SceneMap, TabBar} from 'react-native-tab-view';
import Constants from 'expo-constants';
import PropTypes from 'prop-types';

const FirstRoute = () => (
  <View style={[styles.container, {backgroundColor: '#ff4081'}]} />
);

const SecondRoute = () => (
  <View style={[styles.container, {backgroundColor: '#673ab7'}]} />
);

// 懒加载,当数据还没有出现的时候应该显示的东西
const LazyPlaceholder = ({route}) => (
  <View style={styles.scene}>
    <Text>Loading {route.title}</Text>
  </View>
);

export default class TabViewExample extends React.Component {
  static propTypes = {
    itemWidth: PropTypes.number.isRequired,
  };

  state = {
    index: 0,
    routes: [
      {key: 'first', title: 'First'},
      {key: 'second', title: 'Second'},
    ],
  };

  // 这个加进去就行,不用问为啥
  _handleIndexChange = index => this.setState({index});

  renderRankListEmitter = route => {
    if (route.key === '日榜') {
    } else if (route.key === '周榜') {
    } else if (route.key === '月榜') {
    }
  };

  _renderTabBar = props => {
    const inputRange = props.navigationState.routes.map((x, i) => i);
    const {itemWidth} = this.props;
    return (
      // 上面导航栏的样式
      <TabBar
        scrollEnabled={true}
        {...props}
        style={this.bgC}
        labelStyle={{fontSize: pxToDp(32), fontWeight: 'normal'}}
        activeColor={'#FFFFFF'}
        inactiveColor={'#FFE4D8'}
        indicatorStyle={this.bottomLine}
        tabStyle={{width: 'auto', minWidth: itemWidth}}
        onTabPress={({route, preventDefault}) => {
          this.renderRankListEmitter(route);
        }}
        renderLabel={({route, focused, color}) => (
          <View>
            <Text
              style={{
                fontWeight: focused ? 'bold' : 'normal',
                color,
              }}>
              {route.title}
            </Text>
            <View style={{height: 1}}>
              <Text
                style={{
                  fontWeight: 'bold',
                  color: 'transparent',
                }}>
                {route.title}
              </Text>
            </View>
          </View>
        )}
      />
    );
  };

  // 还是懒加载
  _renderLazyPlaceholder = ({route}) => <LazyPlaceholder route={route} />;

  _renderScene = SceneMap({
    first: FirstRoute,
    second: SecondRoute,
  });

  bottomLine = () => {
    // props中传入的每个item宽度
    const {itemWidth} = this.props;
    return {
      height: 60,
      width: itemWidth * 0.6,
      borderRadius: 30,
      marginLeft: itemWidth * 0.2,
      marginBottom: 18,
      backgroundColor: '#FEC60B',
      elevation: 10,
    };
  };

  render() {
    return (
      <TabView
        navigationState={this.state}
        renderScene={this._renderScene}
        renderTabBar={this._renderTabBar}
        onIndexChange={this._handleIndexChange}
        // 选中的颜色
        activeColor={'#000'}
        // 没有选中的颜色
        inactiveTintColor={'#999'}
        // 选中下划线的样式
        indicatorStyle={this.bottomLine}
        // 每一个tab的宽度
        tabStyle={{width: 90}}
        // 懒加载相关代码
        lazy={true}
        lazyPreloadDistance={1}
        renderLazyPlaceholder={this._renderLazyPlaceholder}
        initialLayout={{width: Dimensions.get('window').width}}
      />
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  tabBar: {
    flexDirection: 'row',
    paddingTop: Constants.statusBarHeight,
  },
  tabItem: {
    flex: 1,
    alignItems: 'center',
    padding: 16,
  },
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值