最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native Android FlatList onScroll Animation - Stack Overflow

programmeradmin8浏览0评论

I have a FlatList with an onScroll function that looks like this:

<Animated.View style={{ transform: [{translateX: this.state.scrollX}] }}>
  <FlatList
    data={ reactionGroup.reactions }
    keyExtractor={ (i) => i.id + uuid.v4() }
    renderItem={ this._renderItem }
    horizontal={ true }
    scrollEventThrottle={1}
    onScroll={ reactionGroup.reactions.length > 1 ? this.onScroll : null }
    showsHorizontalScrollIndicator={ false } />
</Animated.View>

onScroll(event) {
  const { timing } = Animated
  if (event.nativeEvent.contentOffset.x > 0) {
    timing(
      this.state.scrollX,
      { toValue: -60, duration: 100, useNativeDriver: true }
    ).start()
  } else {
    timing(
      this.state.scrollX,
      { toValue: 0, duration: 100, useNativeDriver: true }
    ).start()
  }
},

This works great on iOS, but for Android the animation won't start until the FlatList has stopped scrolling.

I'm basically just kicking off an animation when the user scrolls and setting it back when they go back to the beginning of the horizontal scroll.

Is there a better way to handle this so it works on Android?

I also tried doing Animation.event inside onScroll, but I don't want to tie the animation directly to the scroll position. This approach allowed Android to animate while scrolling, but it was pretty jittery.

RN: 0.43.3

I have a FlatList with an onScroll function that looks like this:

<Animated.View style={{ transform: [{translateX: this.state.scrollX}] }}>
  <FlatList
    data={ reactionGroup.reactions }
    keyExtractor={ (i) => i.id + uuid.v4() }
    renderItem={ this._renderItem }
    horizontal={ true }
    scrollEventThrottle={1}
    onScroll={ reactionGroup.reactions.length > 1 ? this.onScroll : null }
    showsHorizontalScrollIndicator={ false } />
</Animated.View>

onScroll(event) {
  const { timing } = Animated
  if (event.nativeEvent.contentOffset.x > 0) {
    timing(
      this.state.scrollX,
      { toValue: -60, duration: 100, useNativeDriver: true }
    ).start()
  } else {
    timing(
      this.state.scrollX,
      { toValue: 0, duration: 100, useNativeDriver: true }
    ).start()
  }
},

This works great on iOS, but for Android the animation won't start until the FlatList has stopped scrolling.

I'm basically just kicking off an animation when the user scrolls and setting it back when they go back to the beginning of the horizontal scroll.

Is there a better way to handle this so it works on Android?

I also tried doing Animation.event inside onScroll, but I don't want to tie the animation directly to the scroll position. This approach allowed Android to animate while scrolling, but it was pretty jittery.

RN: 0.43.3

Share Improve this question asked Apr 20, 2017 at 15:40 ZachZach 8803 gold badges12 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

You should use the Animated.event approach. As seen in the example in the docs, it maps the event.nativeEvent for you.

Here's a blogpost with an example of how to animate the nav header on scroll by a RN contributor:

https://medium./appandflow/react-native-collapsible-navbar-e51a049b560a

Hope it helps :-)

发布评论

评论列表(0)

  1. 暂无评论