implement tabs in the center of the screen

2019-07-22 07:20发布

I am new to react native. i have implemented stacknavigator inside drawernavigator. Using this library "react-navigation": "^1.0.0-beta.11",

Now i want to implement tabs within the screen at the center. Following image is part of my screen

enter image description here

i dont have any idea how can i do this with any library or manually putting views.

Any help is appreciated. Thanks

1条回答
在下西门庆
2楼-- · 2019-07-22 07:36

Well, I've solved that scenario using react-native-swiper

Basically you have to wrap all views you want to have inside a Swiper, render and style the header as you want.

Here there is a working example I've made:

render() {
    return (
        <View style={styles.body}>
            <Swiper
              height={500}
              showsPagination={true}
              loop={false}
              renderPagination={this._renderPagination}
              ref={component => this._swiper = component}>
                <View style={styles.page}>
                    <FlatList data={..} renderItem={item => ...} keyExtractor={(item, index) => index} />
                </View>
                <View style={styles.page}>
                    <FlatList data={...} renderItem={item => ...} keyExtractor={(item, index) => index} />
                </View>
                <View style={styles.page}>
                    <FlatList data={...} renderItem={item => ...} keyExtractor={(item, index) => index} />
                </View>
                <View style={styles.page}>
                    <FlatList data={...} renderItem={item => ...} keyExtractor={(item, index) => index} />
                </View>
            </Swiper>
        </View>
    );
}

_renderPagination(index, total, context) {
    return (
        <View style={styles.pagination}>
            {this._renderPaginationHeaders(index, total, context)}
        </View>
    )
}

_renderPaginationHeaders(index, total, context) {
    let ret = [];
    for (let i = 0; i < total; i++) {
        ret.push(
            <TouchableOpacity key={i} style={{ flex: 1, flexDirection: 'column' }} onPress={() => this._onPageChange(i)}>
                <Text style={[styles.title, { flex: 1, textAlign: 'center', textAlignVertical: 'center' }]}>
                    {this._getSectionText(i)}
                </Text>
                <View style={{ height: 5, backgroundColor: index === i ? 'magenta' : 'transparent' }}></View>
            </TouchableOpacity>
        );
    }

return ret;
}

_onPageChange(targetIndex) {
    const currentIndex = this._swiper.state.index;
    const offset = targetIndex - currentIndex;
    this._swiper.scrollBy(offset);
}

const styles = StyleSheet.create({
  body: {
    flex: 1,
    alignItems: 'center',
    alignSelf: 'center',
    backgroundColor: '#f1f1f1',
  },
  pagination: {
    flexDirection: 'row',
    width: Dimensions.get('window').width,
    height: Header.currentHeight * 0.7,
    backgroundColor: '#2E2E2E',
    paddingLeft: 2,
    paddingRight: 2,
    alignItems: 'center',
    position: 'absolute',
    top: 0,
  },
  page: {
    flex: 1,
    marginTop: (Header.currentHeight * 0.7) + 3
  },
  title: {
    color: 'white'
  },
});
查看更多
登录 后发表回答