24

React Native ScrollView has a prop pagingEnabled . However, it assumes that the width of each page (or child component) in the ScrollView is equal to the width of the ScrollView.

How can we mend this to make it work correctly for pages that are smaller than the ScrollView?

Is it possible to detect when the user stops scrolling? Then we can easily write our own code to snap to the right page.

Edit: There are a few other ways to mend this by using props that are only available on iOS, so this is particularly a problem on Android.

4 Answers 4

56

There are two different props you can set to React Native ScrollView which takes a callback to notify that scroll has ended. (They are now both documented.)


onScrollEndDrag function

Called as soon as the user lets go of the ScrollView (lifts the finger from the screen).

Working sample: https://rnplay.org/apps/Ufv6Cg (No longer available)


onMomentumScrollEnd function

Called when the ScrollView stops sliding (it will normally continue to slide a little bit after the user has lifted the finger from the screen).

Working sample: https://rnplay.org/apps/BPgG_g (No longer available)


Note: I could not find the methods in the API documentation for any React Native component, but they work as shown in the examples. I saw them used here in react-native-snap-carousel.

Sign up to request clarification or add additional context in comments.

6 Comments

also wanted to add onScrollBeginDrag is another one that triggers at the start of a drag github.com/facebook/react-native/blob/…
oddly enough onScrollEndDrag is not listed anymore on react native API, but still works.
Both your sample links are broken, and they ain't in the Wayback Machine either.
onMomentumScrollEnd is now documented; I've opened PRs to document onScrollEndDrag and onScrollBeginDrag at github.com/facebook/react-native/pull/17368 and github.com/facebook/react-native-website/pull/99.
Nice! I'll update my answer when the new version of the documentation is released (here).
|
2

Another option would be to use the snapToOffsets property which does exactly what you want.

This can be used for paginating through variously sized children that have lengths smaller than the scroll view.

Comments

2

I did it like this:

import React from 'react';
import {ScrollView} from 'react-native';
    
function handleInfinityScroll(event) {
   let mHeight = event.nativeEvent.layoutMeasurement.height;
   let cSize = event.nativeEvent.contentSize.height;
   let Y = event.nativeEvent.contentOffset.y;
   if (Math.ceil(mHeight + Y) >= cSize) return true;
   return false;
}
    
const HomeScreen= ({navigation}) => (
  <ScrollView
    onScroll={({event}) => {
      if (handleInfinityScroll(event)) {
        console.log("END");
      }
    }}
  >
    <></>
  </ScrollView>
);
    
export default HomeScreen;

5 Comments

Can you please tell me how I can play the video which is on display and pause all other videos?
@BilalYaqoob Using react-native-video you can use a state for stop the video after the component has loaded. github.com/react-native-video/react-native-video/issues/…
Yes I know this and I am doing this, But I have a list in flatlist and if I pause all pause and if I run all run, my app is like TikTok feed, I want to detect which is on display so I can play that and pause all others. And when I scroll to the second video I play that video and pause the previous video and also all the other videos.
@BilalYaqoob I got it, u should use refs. videoref.current.setNativeProps({ paused: false }) github.com/react-native-video/react-native-video/issues/2458
Thank you very much for this link, I was finding a solution like that from 3 weeks. I will try applying this solution.
0

As an option, You can use debounce package. And don't forget to clear() it.

import { debounce } from 'debounce';

...

const listenerDebounce = debounce((_) => {
    // Scroll Did End
  }, 160);

return <FlatList
  {...yourProps}
  onScroll={Animated.event(
    [{
        nativeEvent: {
            contentOffset: { y: scrollOffsetValue }
        }
    }],
    {
        useNativeDriver: true,
        listener: listenerDebounce
    },
)
/>;

...

2 Comments

Question was for ScrollView not for FlatList component.
@PinkiDhakad FlatList contains all of the props from ScrollView. Including it's onScroll method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.