react-native-swiper Segement聯(lián)動和Tab頁切換

由于涉及數(shù)據(jù)隱私,對相關(guān)代碼做屏蔽處理,僅限個人學(xué)習(xí)使用。

一、自定義Segement組件

// styles設(shè)置

  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>
    );
  }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容