首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sectionList上的颜色保持随机性

sectionList上的颜色保持随机性
EN

Stack Overflow用户
提问于 2018-04-06 23:26:49
回答 2查看 183关注 0票数 1

嗨,我有一个sectionList,它传递了一个颜色数组+背景索引,随机选择它作为每个行项的背景。当我滑动访问该视图时,颜色会闪烁几秒钟,因为随机选择仍在发生。每次我回到屏幕上的列表,颜色会闪烁几秒钟,然后安定下来。我怎么能没有闪烁和随机选择的颜色发生一次加载?

代码语言:javascript
复制
            class Main extends React.Component {
                constructor(props, context) {
                super(props, context);

                // app state
                this.state = {
                    listColor: [
                    ['rgba(0,36,155,0.8)', 'rgba(26,0,87,0.8)'],
                    ['rgba(155,0,0,0.8)', 'rgba(87,0,0,0.8)']],
                }
                }


            _handleRandomIndex(arr) {
                return arr[Math.floor(Math.random() * arr.length)]
            }

            _renderItem = ({item, section}) => (
                <TouchableScale
                        style={styles.row}
                        onPress={this._onRowPress.bind(this, item)}
                        activeScale={0.94}
                        tension={0}
                        friction={3}
                        >
                        <View style={ styles.elevationLow } borderRadius={9} > 
                            <ImageBackground source={{uri: this._handleRandomIndex(item.bgImgs).image_link }} borderRadius={9} style={ styles.imageBackground }>
                                <LinearGradient
                                        colors={ this._handleRandomIndex(this.state.listColor) }
                                        start={[0.1,0.1]}
                                        end={[0.5,0.5]}
                                        style={{ padding: 20, borderRadius: 9 }}>

                                </LinearGradient>
                            </ImageBackground>
                        </View>  
                        }
                </TouchableScale>
            )
            }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-06 23:38:23

因为每次重呈现都要执行Math.random。因此,每当呈现函数调用时,它都会更改颜色。

改为:

代码语言:javascript
复制
function _handleRandomIndex(arr) {
    return arr[Math.floor(Math.random() * arr.length)]
}

class Main extends React.Component {
    state = {
        listColor: [
            ['rgba(0,36,155,0.8)', 'rgba(26,0,87,0.8)'],
            ['rgba(155,0,0,0.8)', 'rgba(87,0,0,0.8)']
        ]
    }

    _renderItem = ({ item, section }) => <Item item={item} listColor={this.state.listColor} />
}

class Item extends Component {
    rando = _handleRandomIndex(this.props.listColor);

    render() {
        const { item } = this.props;

        return (
            <TouchableScale
                style={styles.row}
                onPress={this._onRowPress.bind(this, item)}
                activeScale={0.94}
                tension={0}
                friction={3}
            >
                <View style={styles.elevationLow} borderRadius={9}>
                    <ImageBackground source={{ uri: this._handleRandomIndex(item.bgImgs).image_link }} borderRadius={9} style={styles.imageBackground}>
                        <LinearGradient
                            colors={this.rando}
                            start={[0.1, 0.1]}
                            end={[0.5, 0.5]}
                            style={{ padding: 20, borderRadius: 9 }}>

                        </LinearGradient>
                    </ImageBackground>
                </View>
            </TouchableScale>
        )
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-04-07 00:55:38

Noitidart的答案是可行的,但所有的项目都将被随机分配到相同的颜色,如果您想让每个项目都有不同的颜色,我认为您需要这样做:

代码语言:javascript
复制
_renderItem = ({item, section}) => {
          const color = this._handleRandomIndex(this.state.listColor);
          return (
            <TouchableScale
                    style={styles.row}
                    onPress={this._onRowPress.bind(this, item)}
                    activeScale={0.94}
                    tension={0}
                    friction={3}
                    >
                    <View style={ styles.elevationLow } borderRadius={9} > 
                        <ImageBackground source={{uri: this._handleRandomIndex(item.bgImgs).image_link }} borderRadius={9} style={ styles.imageBackground }>
                            <LinearGradient
                                    colors={ color }
                                    start={[0.1,0.1]}
                                    end={[0.5,0.5]}
                                    style={{ padding: 20, borderRadius: 9 }}>

                            </LinearGradient>
                        </ImageBackground>
                    </View>  
                    }
            </TouchableScale>
          )
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49702237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档