解决react-native-swiper在安卓上与TabNavigator共用时不显示内容问题
2017-06-18 22:19
567 查看
React Native有一个第三方的组件可以实现轮播功能,但是在开发安卓应用的时候,如果同时使用了react-navigation的TabNavigator导航,会出现轮播点和左右按钮能出现,但是图片内容不显示的问题。
查看了react-native-swiper的github,发现issue中不少人都遇到了这样的问题
issue:https://github.com/leecade/react-native-swiper/issues/389
最后查找资料发现,在安卓机下,如果用到了可滚动组件例如SectionList,ScrollView或者TabNavigator这种可滑动的组建,swiper都无法正确显示,解决办法是设置setTimeout。
以下是实现的一个正常的图片轮播组件:
查看代码可以看到,有这样一段
然后最后render的时候判断this.state.swiperShow,先不会加载图片轮播,等setTimeout执行后改变了状态,图片轮播被渲染,这样就可以正确实现了。
查看了react-native-swiper的github,发现issue中不少人都遇到了这样的问题
issue:https://github.com/leecade/react-native-swiper/issues/389
最后查找资料发现,在安卓机下,如果用到了可滚动组件例如SectionList,ScrollView或者TabNavigator这种可滑动的组建,swiper都无法正确显示,解决办法是设置setTimeout。
以下是实现的一个正常的图片轮播组件:
import React, { Component } from 'react'; import { StyleSheet, Text, View, Image, } from 'react-native'; import Swiper from 'react-native-swiper'; export class ImgSwiper extends Component { constructor(props) { super(props); this.state = { swiperShow:false, }; } componentDidMount(){ setTimeout(()=>{ this.setState({ swiperShow:true }); },0) } render() { if(this.state.swiperShow){ return( <Swiper style={styles.imgWrapper} height={200} showsButtons={true} autoplay={true} > <View style={styles.imgView}> <Image source={ require('../public/images/banner1.jpg')} style={styles.bannerImg} /> </View> <View style={styles.imgView}> <Image source={ require('../public/images/banner2.jpg')} style={styles.bannerImg} /> </View> <View style={styles.imgView}> <Image source={ require('../public/images/banner3.jpg')} style={styles.bannerImg} /> </View> <View style={styles.imgView}> <Image source={ require('../public/images/banner4.jpg')} style={styles.bannerImg} /> </View> </Swiper> ) }else { return ( <View style={{height:200}}> <Image source={ require('../public/images/banner1.jpg')} style={styles.bannerImg} /> </View> ); } } } const styles = StyleSheet.create({ imgWrapper: { width: '100%', height: 200, }, imgView: { flex: 1, height: 200, }, bannerImg: { width: '100%', height: 200, flex: 1 } })
查看代码可以看到,有这样一段
componentDidMount(){ setTimeout(()=>{ this.setState({ swiperShow:true }); },0) }
然后最后render的时候判断this.state.swiperShow,先不会加载图片轮播,等setTimeout执行后改变了状态,图片轮播被渲染,这样就可以正确实现了。
相关文章推荐
- 解决react-native-swiper在安卓上与react navigation TabNavigator一起用时不显示内容问题
- React Native 键盘管理和 Tab Navigator 在Android TextInput遮盖,Tab Navigator上移等问题解决办法
- react-native-tab-navigator 拓展 - 隐藏和显示底部导航
- ListView的头部添加react-native-swiper不显示的问题
- react-native-swiper在滚动视图中默认不显示图片的问题
- 解决react-native的Navigator跳转不刷新页面的问题
- react-native使用Swiper在安卓上不显示
- 没有滚动条,页面内容显示不全问题的解决方法
- 解决表格单元格内容为空时,无法显示单元格边框的问题
- 解决TabContainer选项卡文字显示不全的问题
- 解决Td内容为空时不显示边框的问题-兼容IE、firefox、chrome
- 固定宽度下拉列表中option内容显示不全问题解决方法
- 固定宽度下拉列表中option内容显示不全问题解决方法
- 解决在火狐中注释内容被显示在网页上的问题
- tabcontrol的alignment属性设置成Left或Right时,tabPage的text内容不能显示的问题
- Asp.net中解决母版页CSS样式无法在内容页完整显示——问题
- 固定宽度下拉列表中option内容显示不全问题解决方法
- 解决CHM文件内容无法显示问题
- 使用Response.Redirect跳转时显示提示内容的问题该怎么解决
- Ext中的tabpanel中的项关闭后再打开就不显示的问题经解决