React Native學習(五)—— 使用插件react-native-scrollable-tab-view


本文基於React Native 0.52

Demo上傳到Git了,有需要可以看看,寫了新內容會上傳的。Git地址 https://github.com/gingerJY/React-Native-Demo

一、總覽

如下圖,有一個滑動的tab切換,就是用react-native-scrollable-tab-view來實現的。

 

 

二、使用react-native-scrollable-tab-view插件

1、通過npm將插件加入項目

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

  2、在home.js引入插件

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

  3、使用插件

    ① 這里就寫一下我這個效果的實現,其他內容可以看 https://github.com/skv-headless/react-native-scrollable-tab-view

    ② TabBar有DefaultTabBarScrollableTabBar兩種,這里使用ScrollableTabBar,下面是官方給出的例子(如果是在APP的首頁會出現內容不顯示的問題,三里面有解決辦法)

     <ScrollableTabView
            initialPage={0}
            renderTabBar={() => <ScrollableTabBar />}
      >
            <Text tabLabel='Tab #1'>My</Text>
            <Text tabLabel='Tab #2 word word'>favorite</Text>
            <Text tabLabel='Tab #3 word word word'>project</Text>
            <Text tabLabel='Tab #4 word word word word'>favorite</Text>
            <Text tabLabel='Tab #5'>project</Text>
     </ScrollableTabView>

  

③ 修改默認樣式

  注意改變下划線顏色,要設置backgroundColor

<ScrollableTabView
      renderTabBar={() => <ScrollableTabBar />}
      tabBarBackgroundColor='#fff'
      tabBarActiveTextColor='#b4282d'
      tabBarInactiveTextColor='#333'
      tabBarUnderlineStyle={styles.tabBarUnderline}
>
 …………省略
</ScrollableTabView>

  

tabBarUnderline: {
    backgroundColor: '#b4282d',
    height: 2,
}

    ④ 添加數據

       由於tab項很多,所以數據寫在state里,減少重復代碼,也便於修改

constructor(props) {
   super(props);
   this.state = {
      label: ['推薦', '新品', '居家', '餐廚', '配件', '服裝', '電器', '洗護', '雜貨', '飲食', '嬰童', '志趣'],
   };
}

    ⑤ 遍歷數據  

   renderScrollableTab() {
        let label = this.state.label
            return (
                    <ScrollableTabView
                        renderTabBar={() => <ScrollableTabBar />}
                        …………省略
                    >
                        {
                            label.map((item, index) => {
                                return (<Recommend tabLabel={item} key={index}/>)
                            })
                        }
                    </ScrollableTabView>
            )
    }

       注:Recommend 是一個新加的頁面,注意要在home.js引入。當然也可以不加新頁面,return一個Text也可以。

    home.js完整代碼 https://github.com/gingerJY/example/blob/master/RN_scrollableTab/home.js

三、解決不顯示問題

  APP首頁使用插件經常會出現一些別處沒有的問題,這個tab下面內容不顯示就是其中之一。解決方法為:

  1、加一個是否顯示的狀態    

  constructor(props) {
        super(props);
        this.state = {
            tabShow: false,
        };
  }

  2、在初始化render之后,將狀態設為true

componentDidMount() {
   setTimeout(() => {
     this.setState({
         tabShow: true
     });
   }, 0)
}

  3、render的時候判斷this.state.tabShow,等setTimeout執行后改變了狀態,才會渲染

   if (this.state.tabShow){
            return (
                <ScrollableTabView
                    renderTabBar={() => <ScrollableTabBar />}
                   …………略
                >
                    …………略
                </ScrollableTabView> 
            )
     }

  這樣就可以顯示了。

END--------------------------------------------------

生命周期、布局


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM