【问题标题】:flat list android animation doesn't work as expected平面列表 android 动画无法按预期工作
【发布时间】:2018-05-28 13:56:13
【问题描述】:

我正在尝试在 flatList 中的项目上制作动画。 每个项目将从右到左出现。 在 IOS 上可以正常工作,但在 ANDROID 上不行。

动画包含淡入和变换:translateX 动画。

如果有其他方法可以在 IOS 上点赞我会很高兴,它应该是一个动态列表

FadeInView 组件:

export class FadeInView extends Component {
state = {
    rightToLeftAnim: new Animated.Value(450),
    fadeAnim: new Animated.Value(0),
};

componentDidMount() {
    Animated.sequence([
        Animated.delay((this.props.index + 1) * 1000),
        Animated.parallel([
            Animated.timing(
                this.state.rightToLeftAnim,
                {
                    toValue: 0,
                    duration: 1000,
                    easing: Easing.inOut(Easing.quad),
                    useNativeDriver: true,
                }
            ),
            Animated.timing(
                this.state.fadeAnim,
                {
                    toValue: 1,
                    duration: 1000,
                    useNativeDriver: true,
                }
            )
        ])
    ]).start();

}

render() {
    let { fadeAnim,rightToLeftAnim } = this.state;

    const rightToLeft = {
        transform: [{translateX: rightToLeftAnim}]
    };

    return (
        <Animated.View                
            style={[rightToLeft,{flex: 1, ...this.props.style, opacity: fadeAnim,}]}
        >
            {this.props.children}
        </Animated.View>
    );
}

}

父组件:

 class MyPlans extends Component {
  constructor(props) {
    super(props);
   }
    render() {
    return (
        <View>
                <FlatList
                    style={{marginTop: Platform.OS === 'ios' ? 0 : 60}}
                    data={[{key: 'module a'},{key: 'module b'},{key: 'module c'},{key: 'module d'}]}
                    horizontal={true}
                    renderItem={({item,index}) => <FadeInView index={index} style={this.moduleStyle}><Text>{item.key}</Text></FadeInView>}
                />
        </View>);
}

【问题讨论】:

    标签: react-native react-native-android react-native-flatlist react-native-animatable


    【解决方案1】:

    style={{flex:1}} 给View Component,它是FlatList 的父级。

    return (
            <View style={{flex:1}}>
                    <FlatList
                        style={{marginTop: Platform.OS === 'ios' ? 0 : 60}}
                        data={[{key: 'module a'},{key: 'module b'},{key: 'module c'},{key: 'module d'}]}
                        horizontal={true}
                        renderItem={({item,index}) => <FadeInView index={index} style={this.moduleStyle}><Text>{item.key}</Text></FadeInView>}
                    />
            </View>);
    

    【讨论】:

      【解决方案2】:

      请使用 flatList getItemLayou 属性,或更新 rn 版本到 0.59.5 以上。

      【讨论】:

      • 这是不可接受的答案,请添加您建议的方法的完整说明,或使用评论代替
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      相关资源
      最近更新 更多