react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[FlatList] Android: onMomentumScrollEnd doesn't trigger when use scrollToOffset

Open myl0204 opened this issue 5 years ago • 58 comments

  • [x] Review the documentation: https://facebook.github.io/react-native
  • [x] Search for existing issues: https://github.com/facebook/react-native/issues
  • [x] Use the latest React Native release: https://github.com/facebook/react-native/releases

Environment

React Native Environment Info: System: OS: macOS High Sierra 10.13.6 CPU: x64 Intel(R) Core(TM) i5-5675R CPU @ 3.10GHz Memory: 60.77 MB / 8.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 8.11.1 - /usr/local/bin/node Yarn: 1.9.4 - /usr/local/bin/yarn npm: 5.6.0 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 5.0 IDEs: Android Studio: 3.0 AI-171.4443003 Xcode: 10.0/10A255 - /usr/bin/xcodebuild npmPackages: react: 16.5.0 => 16.5.0 react-native: 0.57.2 => 0.57.2

Description

onMomentumScrollEnd doesn't trigger when use scrollToOffset in Android.

And in iOS it works.

also i find that other methods e.g. scrollToIndex, have the same issue.

Reproducible Demo

export default class App extends Component<Props> {
  renderItem = ({item: { source }}) => {
    return (
      <TouchableOpacity onPress={() => {this.scrollView.scrollToOffset({offset: 375, animated: true})}}>
        <Image
          source={{uri: source.uri}}
          style={{width: 375, height: 375}}
        />
      </TouchableOpacity>
    )
  }

  onMomentumScrollEnd = () => {
    console.log('scrollend')
  }

  render() {
    return (
      <FlatList
        style={{ flex: 1, backgroundColor: 'black' }}
        data={[
          { source: { uri: 'http://i.imgur.com/XP2BE7q.jpg' } },
          { source: { uri: 'http://i.imgur.com/5nltiUd.jpg' } },
          { source: { uri: 'http://i.imgur.com/6vOahbP.jpg' } },
          { source: { uri: 'http://i.imgur.com/kj5VXtG.jpg' } }
        ]}
        renderItem={this.renderItem}
        ref={view => this.scrollView = view}
        onMomentumScrollEnd={this.onMomentumScrollEnd}
        scrollEnabled={true}
        horizontal={true}
      />
    );
  }
}

myl0204 avatar Oct 11 '18 06:10 myl0204

Can you run react-native info and edit your issue to include these results under the Environment section?

If you believe this information is irrelevant to the reported issue, you may write [skip envinfo] under Environment to let us know.

react-native-bot avatar Oct 11 '18 07:10 react-native-bot

did anyone have check this issue? just let me know there is somebody following this. Or, maybe some of you can tell me that's not an issue with react-native itself. Thanks!

myl0204 avatar Oct 23 '18 12:10 myl0204

I am also currently having this issue with react native 0.57.5

mccjul avatar Dec 10 '18 15:12 mccjul

ANyone has a workaround for this issue?

otusweb avatar Dec 16 '18 14:12 otusweb

+1

leolusoli avatar Jan 07 '19 18:01 leolusoli

Having this issue as well.

alexandrecanuto avatar Jan 16 '19 20:01 alexandrecanuto

+1 on react-native 0.57.8v

csath avatar Feb 15 '19 07:02 csath

I was running into this issue also on 0.57.1. In my case, I was trying to use the scrollTo method on a Scroll View. I wanted to trigger something after scrolling to a specific location on the screen. I was able to use the onScroll prop to achieve this 😄

onScroll(event) {
     if (event.nativeEvent.contentOffset.y === this.targetScreenOffsetY) {
        this.doSomething();
     }
}

joseph-perez avatar Mar 06 '19 03:03 joseph-perez

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

stale[bot] avatar Aug 04 '19 19:08 stale[bot]

I am also currently having this issue with react native 0.59

IseeUDenis avatar Aug 26 '19 07:08 IseeUDenis

I am also currently having this issue with react native 0.61.2 @joseph-perez thanks for workaround!

Ryabchikus avatar Nov 06 '19 11:11 Ryabchikus

I feel really bad to use scrollview in react-native.Another bug,onMomentumScrollEnd not work when scroll slowly on ios.

gwl002 avatar Dec 10 '19 03:12 gwl002

I faced with it. I also use horizontal FlatList. onMomentumScrollEnd very often not called if stop scroll with small velocity.

Bardiamist avatar Jan 16 '20 05:01 Bardiamist

I accidentally found a solution for myself. I wrapped every FlatList item in TouchableOpacity.

Bardiamist avatar Jan 17 '20 03:01 Bardiamist

Same problem in 0.61.5, scrollToOffset didn't trigger onMomentumScrollEnd callback. @Bardiamist no luck for me, it didn't work 😢

PaxLyj avatar Jan 31 '20 05:01 PaxLyj

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

stale[bot] avatar Apr 30 '20 06:04 stale[bot]

+1

ManAnRuck avatar May 02 '20 11:05 ManAnRuck

+1

gibrangul avatar May 10 '20 14:05 gibrangul

still facing this issue even with a ScrollView as well, it some times trigger but very rarly and not consistent ...

a-eid avatar May 13 '20 20:05 a-eid

still reproduce this issue in 0.57.7

Jaycexx avatar Jul 16 '20 07:07 Jaycexx

i think i can shed some light to when onMomentumScrollEnd does not trigger on android as of RN0.61.5 when dragged manually (and released without velocity), albeit only little related to this issue. that might sound weird but after fiddling for hours to fix this issue, we solved it by replacing margins with paddings on the children and each child needed a backgroundColor property, whether with 'transparent' or a color. not setting this property made the bug reproducible again and we weren't able to get the onMomentumScrollEnd event to fire.

  • if children within a FlatList / ScrollView are having margins and the drag appears to be finished on a margin, say "Dead Space", onMomentumScrollEnd is not fired on Android. we found that by adding backgroundColors to each child and whenever we left the drag on a not-background area, no event was fired

  • if children within a FlatList / ScrollView are having fixed sizes and paddings yet no backgroundColor is defined and the drag appears to be finished on a Dead Space, onMomentumEnd is not fired on Android (funnily enough, if they're given backgroundColor: 'transparent', the event is fired even though the actual item's padding was hit)

i'm trying to provide a working demo for this as currently we fixed it in a non-abstractable screen.

amblicity avatar Sep 03 '20 12:09 amblicity

any work arounds to get onMomentumScrollEnd to fire after a call to scrollToOffset?

ajpaulingalls avatar Nov 10 '20 21:11 ajpaulingalls

I have added a PR to address this. If anyone here could test it or review it and comment on the PR, I'd love to get some momentum to help it get merged. Thanks!

ajpaulingalls avatar Nov 12 '20 05:11 ajpaulingalls

this issue still exists on 0.64 but it causes to trigger 4 times

esinanturan avatar May 05 '21 12:05 esinanturan

It exists in 0.64, scrollToIndex does not fire onMomentumScrollEnd at the end of scrolling. It is called when user drags it but not when scrollToIndex is used.

mohity777 avatar May 14 '21 18:05 mohity777

I have found a tricky workaround for this - you can use onScroll instead and check for some conditions.

For instance, check if paginated scroll completed scrolling to the next page with if (event.nativeEvent.contentOffset % Dimensions.get('window').width === 0) { %something you wanted to trigger in onMomentumScrollEnd% }

milky-grech avatar May 28 '21 09:05 milky-grech

Still having this problem on Android, is there no workaround for using onMomentumScrollEnd together with scrollToOffset? I am using onMomentumScrollEnd to detect the index of the current item, then when a button is pressed the pagination/index is incremented.

LukeAppGuy avatar Jul 12 '21 15:07 LukeAppGuy

I didn't found any answered thread so I got a way to solve it.

      flatList?.current?.scrollToOffset({
        offset: (currentIndex + 1) * width,
        animated: true,
      }); // This will trigger the onMomentunScrollEnd on iOS.
      if (Platform.OS === 'android') {
        setCurrentIndex(currentIndex + 1); // this is what we trigger on the onMomentunScrollEnd
      }

This problem only happens on Android, so you can manually trigger the update that's not being call on android.

SGarcia710 avatar Aug 29 '21 07:08 SGarcia710

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

stale[bot] avatar Jan 09 '22 13:01 stale[bot]

actual

exzos28 avatar Apr 01 '22 14:04 exzos28