• react native中使用ScrollableTabView


    第一步,下载依赖

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

    第二步,引入

    import ScrollableTabView, { ScrollableTabBar, DefaultTabBar } from 'react-native-scrollable-tab-view';

    第三步,使用

    class TabTopView extends Component {
        render() {
            return (
                <ScrollableTabView
                    style={styles.container}
                    renderTabBar={() => <DefaultTabBar />}
                    tabBarUnderlineStyle={styles.lineStyle}
                    tabBarActiveTextColor='#FF0000'>
     
                    <Text style={styles.textStyle} tabLabel='娱乐'>娱乐</Text>
                    <Text style={styles.textStyle} tabLabel='科技'>科技</Text>
                    <Text style={styles.textStyle} tabLabel='军事'>军事</Text>
                    <Text style={styles.textStyle} tabLabel='体育'>体育</Text>
                </ScrollableTabView>
            );
        }

    由于我把里面的一层提出来循环了,所以在上面使用方法的时候直接找了另一种方法

    效果图是

    完整代码是 home.js

    import React, { Component } from 'react';
    import {  StyleSheet,View,Text,Dimensions } from 'react-native';
    import ScrollableTabView, { ScrollableTabBar, DefaultTabBar } from 'react-native-scrollable-tab-view';
    
    // 取得屏幕的宽高Dimensions
    const { width, height } = Dimensions.get('window');
    
    export default class App extends Component{
        constructor(props) {
            super(props);
            this.state = {
                tabShow: false,
                label: ['推荐', '新品', '居家', '餐厨', '配件', '服装', '电器', '洗护', '杂货', '饮食', '婴童', '志趣'],
            };
        }
    
        componentDidMount() {
            setTimeout(() => {
                this.setState({
                    tabShow: true
                });
            }, 0)
        }
        // 滑动tab
        renderScrollableTab() {
            let label = this.state.label
            if (this.state.tabShow){
                return (  
                    <ScrollableTabView
                            renderTabBar={() => <ScrollableTabBar />}
                            tabBarBackgroundColor='#ddd'
                            tabBarActiveTextColor='#b4282d'
                            tabBarInactiveTextColor='#333'
                            tabBarUnderlineStyle={styles.tabBarUnderline}
                    >
                            {
                                label.map((item, index) => {
                                    if (item == '推荐') {
                                        return (
                                            <Text tabLabel={item} key={index}>推荐</Text>
                                        )
                                    } else {
                                        return (
                                            <Text tabLabel={item} key={index}>其他页面</Text>
                                        )
                                    }
                                })
                            }
                    </ScrollableTabView>
                )
            }   
        }
    
        render(){
            return(
                <View style={styles.container}>
                    <View style={{ flex: 1 }}>
                        {this.renderScrollableTab()}
                    </View>
                </View>
            )
        }
    }
    const styles = StyleSheet.create({
        container: {
            flex: 1,
            alignItems: 'center',
            backgroundColor: '#efefef',
        },
        navLeft: {
            alignItems: 'center',
            marginLeft: 10,
        },
        navRight: {
            alignItems: 'center',
            marginRight: 10,
        },
        navIcon: {
            height: 20,
             20,
        },
        navText: {
            fontSize: 10,
        },
        searchBox: {
            flexDirection: 'row',
            alignItems: 'center',
            justifyContent: 'center',
             width * 0.7,
            backgroundColor: '#ededed',
            borderRadius: 5,
            height: 30,
        },
        searchIcon: {
             16,
            height: 16,
            marginRight: 6,
        },
        searchContent: {
            color: '#666',
            fontSize: 14,
        },
        tabBarUnderline: {
            backgroundColor: '#b4282d',
            height: 2,
        }
    });
  • 相关阅读:
    SQL常见问题及解决备忘
    工厂方法模式-Factory Method
    访问者模式-Visitor
    解释器模式-Interpreter
    享元模式-Flyweight
    系统的重要文件/etc/inittab被删除了--急救办法!
    Database基础(二):MySQL索引创建与删除、 MySQL存储引擎的配置
    轻松解决U盘拷贝文件时提示文件过大问题
    Cisco基础(六):配置目前网络环境、项目阶段练习
    Cisco基础(五):配置静态NAT、配置端口映射、配置动态NAT、PAT配置、办公区Internet的访问
  • 原文地址:https://www.cnblogs.com/ldlx-mars/p/9361688.html
Copyright © 2020-2023  润新知