美文网首页
react-native-swiper Segement联动和T

react-native-swiper Segement联动和T

作者: 金丝楠 | 来源:发表于2019-10-21 10:22 被阅读0次

    由于涉及数据隐私,对相关代码做屏蔽处理,仅限个人学习使用。

    一、自定义Segement组件

    // styles设置
    
      selectContainer: {
        marginTop: 6,
        flexDirection: "row",
        justifyContent: "center",
        alignItems: "center",
        backgroundColor: "#5685E4",
        width: 72,
        height: 32,
        borderColor: "#5685E4",
        borderWidth: 1
      },
      unSelectContainer: {
        marginTop: 6,
        flexDirection: "row",
        justifyContent: "center",
        alignItems: "center",
        backgroundColor: "#FFFFFF",
        width: 72,
        height: 32,
        borderColor: "#5685E4",
        borderWidth: 1
      },
      selectTitle: {
        color: "#FFFFFF",
        fontSize: font(12)
      },
      unSelectTitle: {
        color: "#5685E4",
        fontSize: font(12)
      }
    
    

    render和点击事件

      constructor(props) {
        super(props);
        this.state = {
          isOutTab: true
        };
      }
    
      onOutTab = () => {
        this.setState({
          isOutTab: true
        });
      };
    
      onInTab = () => {
        this.setState({
          isOutTab: false
        });
      };
    
      renderSwitch = () => {
        const { isOutTab } = this.state;
        return (
          <View style={styles.container}>
            <TouchableOpacity onPress={this.onOutTab}>
              <View
                style={isOutTab ? styles.selectContainer : styles.unSelectContainer}
              >
                <Text style={isOutTab ? styles.selectTitle : styles.unSelectTitle}>
                  ATab
                </Text>
              </View>
            </TouchableOpacity>
            <TouchableOpacity onPress={this.onInTab}>
              <View
                style={isOutTab ? styles.unSelectContainer : styles.selectContainer}
              >
                <Text style={isOutTab ? styles.unSelectTitle : styles.selectTitle}>
                  BTab
                </Text>
              </View>
            </TouchableOpacity>
          </View>
        );
      };
    
    

    二、Tab分页使用react-native-swiper第三方组件

    import Swiper from "react-native-swiper";
    
      renderTabs = () => {
        const { isOutTab } = this.state;
        return (
          <Swiper
            style={{ width: screenW * 2, height: isOutTab ? 1200 : 440 }}
            showsButtons={false}
            loop={false}
            bounces
            horizontal
            showsPagination={false}
            index={isOutTab ? 0 : 1}
            scrollEventThrottle={0}
            onIndexChanged={index => {
              if (index === 1) {
                this.onInTab();
              } else {
                this.onOutTab();
              }
            }}
          >
            <View>
              <VoucherList
                data={realtimeData}
                voucherMarkTitle="标题A"
                voucherMarkSubTitle="副标题A"
              />
              <VoucherList
                data={biddingData}
                voucherMarkTitle="标题B"
                voucherMarkSubTitle="副标题B"
              />
              <VoucherList
                data={externalData}
                voucherMarkTitle="标题C"
                voucherMarkSubTitle="副标题C"
              />
            </View>
            <FinancialInList data={data} />
          </Swiper>
        );
      };
    
      render() {
        return (
          <View>
            <View style={styles.separator} />
            {this.renderSwitch()}
            {this.renderTabs()}
          </View>
        );
      }
    
    

    相关文章

      网友评论

          本文标题:react-native-swiper Segement联动和T

          本文链接:https://www.haomeiwen.com/subject/blgzmctx.html