【问题标题】:Animate Single item on flatlist为平面列表上的单个项目设置动画
【发布时间】:2019-09-20 15:16:51
【问题描述】:

我如何为平面列表中的单个项目设置动画?

我尝试添加一个值来隔离选定的记录,但没有奏效,也许我写错了。 Atm 动画在所有记录中

平面列表:

 <View style={style.container}>
                <FlatList
                    data={this.state.ReturnedArray}
                    width='100%'
                    ItemSeparatorComponent={this.FlatListItemSeparator}
                    renderItem={this.renderizza}>

渲染Flalist:

renderizza = (item) => {
        var str = JSON.parse(JSON.stringify(item.item.itemType)).name + ' ' + JSON.parse(JSON.stringify(item.item.itemType)).cognome
        let acronym = str.split(/\s/).reduce((response, word) => response += word.slice(0, 1), '')
        return (
            <View style={style.containerFlat}>
                <View style={style.containerFlat1}>    
                    <Text style={style.txt}>{JSON.parse(JSON.stringify(item.item.itemType)).name} {JSON.parse(JSON.stringify(item.item.itemType)).cognome}</Text>
                </View>
               <TouchableOpacity style={style.containerFlat2} onPress={() => this.badge(JSON.parse(JSON.stringify(item.item.itemType)).expoToken)}> 
                    <Animated.View style={[style.animatedView, { opacity: this.state.fadeValue}]}><Text>ADDED</Text></Animated.View>
                    <Text style={style.AvatarTxt}  >{acronym}</Text>
                </TouchableOpacity>
            </View>
        )
    }

这里是动画:

    badge(chiavi) {
        console.log(chiavi)
        if ((this.state.SelectedUser).includes(chiavi)) {
            this.state.SelectedUser.splice(this.state.SelectedUser.indexOf(chiavi), 1)
            Animated.timing(this.state.fadeValue, {
                toValue: 0,
                duration: 0
            }).start();
        } else {
            this.state.SelectedUser.push(chiavi)
            Animated.timing(this.state.fadeValue, {
                toValue: 1,
                duration: 0
            }).start()
        }
    }

热修复? 谢谢

【问题讨论】:

    标签: react-native react-native-flatlist


    【解决方案1】:

    一种方法是在状态下保持一个活动项,并在Flatlist 中为这个变量使用extraData 属性。然后有条件地在动画项和普通项之间渲染。

    animate = index => {
        this.setState({
          activeItem: index,
        });
         //Rest of animation code.
      };
    
    <TouchableOpacity onPress={e => this.animate(index)}>
       {this.state.activeItem === index && (
          <Animated.View
             style={[
                    styles.button,
                    { transform: [{ scale: this.animationMap }] },
                    ]}
          >
            <Text>{item.title}</Text>
          </Animated.View>
          )
       }
       {this.state.activeItem !== index && (
              <View style={styles.button}>
                <Text>{item.title}</Text>
              </View>
            )
       }
     </TouchableOpacity>
    

    这是一个示例Demo

    【讨论】:

    • 希望你理解清楚,因为我没有添加任何cmets。
    • 不幸的是,这项工作由单个项目完成。我需要多项选择。将环顾四周
    • 虽然了解如何适应我的代码,但一无所获。介意你可以帮助我的代码吗?在事件 Onpress 中,我将令牌添加到数组中,如果已经预设则删除。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多