react-native-scrollable-tab-view 嵌套react-native-swiper 引发的 bug


问题描述:当 swiper 嵌套在react-native-scrollable-tab-view中时,在 ios 端正常,但在 Android 端会导致 swiper 子视图不出现
swiper 异常
打开 swiper 和scrollable的源码发现,在 Android 端使用的是
viewpagerAndroid, 猜测是因为viewpagerAndroid的嵌套导致的问题,于是写代码测试

      <ViewPagerAndroid  initialPage={0} style={{height:100}}>
        <View style={{backgroundColor:'red'}}>
            <Text style={{fontSize:50,color:'white'}}>aa</Text>
        </View>
        <View style={{backgroundColor:'blue'}}>
            <ViewPagerAndroid initialPage={0} style={{height:100}}>
              <View  style={{backgroundColor:'yellow'}}>
                <Text style={{fontSize:50,color:'white'}}>bb</Text>
              </View>
              <View style={{backgroundColor:'green'}}>
                <Text style={{fontSize:50,color:'white'}}>cc</Text>
              </View>
            </ViewPagerAndroid>
        </View>
      </ViewPagerAndroid>

运行结果如下:
这里写图片描述
这里写图片描述
左右滑动发现只有red 和 blue 背景的视图, bb 和 cc 文案也没有,表明第二个 ViewPagerAndroid没有渲染

在 github react native上查询,发现有很多类似问题 ViewPagerAndroid nest issue
看了之后使用了其中几个方法都无效,但是给了一个灵感,以下是解决方案:
在第二个 viewpagerAndroid 添加一个 key
首先添加一个 state

    this.state = {
      key:0
    };


      <ViewPagerAndroid  initialPage={0} style={{height:100}}>
        <View style={{backgroundColor:'red'}}>
            <Text style={{fontSize:50,color:'white'}}>aa</Text>
        </View>
        <View style={{backgroundColor:'blue'}}>
            <ViewPagerAndroid key={this.state.key} initialPage={0} style={{height:100}}>
              <View  style={{backgroundColor:'yellow'}}>
                <Text style={{fontSize:50,color:'white'}}>bb</Text>
              </View>
              <View style={{backgroundColor:'green'}}>
                <Text style={{fontSize:50,color:'white'}}>cc</Text>
              </View>
            </ViewPagerAndroid>
        </View>
      </ViewPagerAndroid>

在componentDidMount强制刷新视图

  componentDidMount() {
    this.timer = setTimeout(() => {this.setState({key:1})}, 0);
  }

运行结果:
这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值