账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    react-native:react-native-scrollable-tab-view抖动
    23
    0

    问题描述

    项目使用了react-native-scrollable-tab-view来实现tab栏位切换,发现在debug的时候内容会不停抖动(较大几率出现),发布到正式生产环境目前还没有碰到过。

    问题出现的环境背景及自己尝试过哪些方法

    尝试发现,如果react-native-scrollable-tab-view中固定每一个标签页的宽度,就不会抖动(这个是通过反复加载来确认的),但由于标签页显示的文字是后台设置的,不能采用简单的固定每个标签页宽度来解决这个问题
    

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)

                {/*顶部tabView滑动切换布局*/}
                {this.state.boardList.length > 0 ? (
                    <View style={styles.tabBgStyle}>
                            <ScrollableTabView
                                ref={ref => {
                                    this.scrollableTabView = ref;
                                }}
                                renderTabBar={() => <ScrollableTabBar style={styles.tabStyle}/>}
                                tabBarUnderlineStyle={styles.tabBarLine}
                                tabBarActiveTextColor={global.homeColor}
                                tabBarInactiveTextColor="#666666"
                                tabBarTextStyle={styles.tabBarTextStyle}
                                onChangeTab={(obj) =>this.handleChangeTab(obj)}
                            >
                                {this.state.boardList.map((v, i) => this.renderBoardItem(v, i))}
                            </ScrollableTabView>
                            <TouchableOpacity
                                onPress={() => {
                                    NoDoublePress.onPress(() => {
                                        this.refs.modalRef.open();
                                    })
                                }}
                                style={{
                                    width: 40,
                                    height: 46,
                                    alignItems: "center",
                                    justifyContent: "center",
                                    backgroundColor: "#fff",
                                    position: "absolute",
                                    right: 0,
                                    top: 0,
                                    opacity: 0.8
                                }}
                            >
                                <Image source={Shape} style={{width: 17, height: 15}}/>
                            </TouchableOpacity>
                        <View style={{
                            backgroundColor: '#f3f4f5',
                            height: 1,
                            width: '100%',
                            position: 'absolute',
                            top: 46
                        }}>
                        </View>
                    </View>
                ) : null}
               // 相关style
                   tabBgStyle: {
        backgroundColor: '#ffffff',
        flex:1,
        flexDirection:'column',
        alignItems: 'center',
        justifyContent: 'flex-start',
    },
    tabStyle: {
        backgroundColor: '#ffffff',
        borderWidth: 0,
        width: '100%',
        height: 46,
        alignItems: 'center',
        justifyContent: 'center',
        paddingRight:40,// 安卓上直接设置width:width-40会出现左边有留白的问题,可以用paddingRight来设置右边
    },
    
    tabBarTextStyle: {
        width:'100%',// debug下测试如果宽度写死固定宽度,可以避免抖动的问题发生。
        flexDirection:'column',
        alignItems: 'center',
        justifyContent: 'center',
        fontSize:setSpText(16),
    },
    
    tabBarLine: {
        width:0,
        height:0,
    },
    

    你期待的结果是什么?实际看到的错误信息又是什么?

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 旧⊙人往事 普通会员 1楼

      在React Native中,react-native-scrollable-tab-view是一个用于显示多行滚动视图的组件。如果你在使用这个组件时发现它有抖动现象,这可能是因为你在更改视图的组件树时,可能会引起某些组件的布局或性能问题。

      解决这个问题的一种方法是使用React.memoshouldComponentUpdate生命周期方法来防止组件树的更新。这些方法可以帮助你在更新视图之前检查组件的状态,从而防止性能问题。

      以下是一个使用React.memo的例子:

      ```jsx import React from 'react'; import { View, Text } from 'react-native';

      const TabView = React.memo(function(TabView) { return ( {TabView.content} ); });

      export default TabView; ```

      在这个例子中,我们使用了React.memo来修饰TabView组件。这个组件只有在它的content属性没有改变的情况下才会被重新渲染。这样,即使content属性的值发生了改变,也不会导致组件树的更新。

      另一种方法是使用shouldComponentUpdate生命周期方法。这是一个更简洁的方法,但可能会更难以理解。以下是一个使用shouldComponentUpdate的例子:

      ```jsx import React from 'react'; import { View, Text } from 'react-native';

      const TabView = React.memo(function(TabView) { return ( {TabView.content} ); });

      export default TabView; ```

      在这个例子中,我们使用了shouldComponentUpdate来检查TabView组件的状态。如果它的状态发生了改变,那么它就会重新渲染组件,而不是等待视图的内容改变。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部