react-native-scrollable-tab-view組件的簡單使用

react-native-scrollable-tab-view是一個(gè)滑動tab組件,可在tab之間進(jìn)行切換顯示內(nèi)容
https://github.com/skv-headless/react-native-scrollable-tab-view

1、安裝依賴

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

2、引入組件

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

3、組件使用
系統(tǒng)給我們提供了以下默認(rèn)兩種樣式 ,當(dāng)然我們也可以自定義
ScrollableTabBar :Tab可以超過屏幕范圍,滾動可以顯示
DefaultTabBar : Tab會平分在水平方向的空間

a.使用DefaultTabBar 樣式,Tab平分在水平方向的空間

render() {
    return (
<ScrollableTabView
    renderTabBar={() => <DefaultTabBar/>}>
    <Text tabLabel='Tab1'/>
    <Text tabLabel='Tab2'/>
    <Text tabLabel='Tab3'/>
    <Text tabLabel='Tab4'/>
    <Text tabLabel='Tab5'/>
    <Text tabLabel='Tab6'/>
</ScrollableTabView>
);
}

效果如下:

Paste_Image.png

b.使用ScrollableTabBar 樣式,Tab超過屏幕范圍,滾動顯示

render() {
    return (
<ScrollableTabView
    style={styles.container}
    tabBarPosition='top'//tabBarPosition默認(rèn)top  位于屏幕頂部   bottom位于屏幕底部  overlayTop懸浮在頂部
    initialPage={0} //初始化時(shí)被選中的Tab下標(biāo),默認(rèn)是0
    locked={false}//表示手指是否能拖動視圖  默認(rèn)false  true則不能拖動,只可點(diǎn)擊
    renderTabBar={() => <ScrollableTabBar />}
    tabBarUnderlineStyle={{backgroundColor: '#FF0000'}}//設(shè)置DefaultTabBar和ScrollableTabBarTab選中時(shí)下方橫線的顏色
    tabBarBackgroundColor='#FFFFFF'//設(shè)置整個(gè)Tab這一欄的背景顏色
    tabBarActiveTextColor='#9B30FF'//設(shè)置選中Tab的文字顏色
    tabBarInactiveTextColor='#7A67EE'//設(shè)置未選中Tab的文字顏色
    tabBarTextStyle={{fontSize: 18}}//設(shè)置Tab文字的樣式
    onChangeTab={(obj) => {//Tab切換之后會觸發(fā)此方法
      console.log('index:' + obj.i);
    }}
    onScroll={(postion) => {  //視圖正在滑動的時(shí)候觸發(fā)此方法
      // float類型 [0, tab數(shù)量-1]
      console.log('scroll position:' + postion);
    }}
>
    <View tabLabel='Tab1'>
        <Text>Tab1</Text>
    </View>
    <View tabLabel='Tab2'>
        <Text>Tab2</Text>
    </View>
    <View tabLabel='Tab3'>
        <Text>Tab3</Text>
    </View>
    <View tabLabel='Tab4'>
        <Text>Tab4</Text>
    </View>
    <View tabLabel='Tab5'>
        <Text>Tab5</Text>
    </View>
    <View tabLabel='Tab6'>
        <Text>Tab6</Text>
    </View>
</ScrollableTabView>
);
}

效果如下:

Paste_Image.png

這里有個(gè)問題,在使用ScrollableTabBar 樣式的時(shí)候,假如我們設(shè)置了tabBarUnderlineStyle選中狀態(tài)下劃線顏色,默認(rèn)選中第一個(gè)tab沒有選中下劃線樣式,在點(diǎn)擊tab的時(shí)候才會出現(xiàn)下劃線樣式,而使用DefaultTabBar樣式時(shí)沒有這種問題,不知道是什么原因,有知道原因的還請告知一下,謝謝。

Paste_Image.png

參考資料:

作者:fozero
聲明:原創(chuàng)文章,轉(zhuǎn)載請注意出處!http://www.itdecent.cn/p/a729fa3c4754
標(biāo)簽:ReactNative
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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