Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native ScrollView - How to scroll items one by one?

How do you disable inertia scrolling using React Native ScrollView, so that whatever force you apply, it will be next element that the list is going to be scrolled to (and snapped to)? I have reviewed the list of props and any of them does directly what I would like to achieve.

like image 879
Michał Urbaniak Avatar asked Apr 14 '18 09:04

Michał Urbaniak


People also ask

How do I scroll specific sections in my React Native app?

Now, whenever we want to scroll to a specific location we can use scrollTo which is a property of ScrollView. In this, we have to pass the X and Y location to scroll and animated (True/False).

How do you make a scrollable list in React Native?

Hence, to make a scrollable list of data, you also need to put the View component inside the ScrollView component. Note: ScrollView must have a parent with a defined height. The ScrollView is a generic React Native scrolling container that allows both vertical and horizontal direction scrolling.

How do I add a horizontal scroll in React Native?

In the ScrollView, we can scroll the components in both direction vertically and horizontally. By default, the ScrollView container scrolls its components and views in vertical. To scroll its components in horizontal, it uses the props horizontal: true (default, horizontal: false).


2 Answers

Use disableIntervalMomentum={ true } in your ScrollView. This will only allow the user to scroll one page at a time horizontally.

https://reactnative.dev/docs/scrollview#disableintervalmomentum

<ScrollView 
  horizontal
  disableIntervalMomentum={ true } 
  snapToInterval={ width }
>
  <Child 1 />
  <Child 2 />
</ScrollView>
like image 147
Scott Avatar answered Oct 13 '22 20:10

Scott


Assuming that you want to snap to an item horizontally or vertically, its position needs to be fixed relative to the screen (where it should snap)

Since the props are available for IOS only therefore

You can use

  • decelerationRate- Set the de accelaration rate to 0, once the user lifts the finger
  • snapToAlignment - Set the alignmnet to a particular element to center
  • snapToInterval - Set the interval to snap to based on your contentInset props.

    <ScrollView 
        horizontal
        decelerationRate={0}
        snapToInterval={width - (YOUR_INSET_LEFT + YOUR_INSET_RIGHT)}
        snapToAlignment={"center"}
        contentInset={{top: 0, left: YOUR_INSET_LEFT, bottom: 0, right: YOUR_INSET_RIGHT,
        }}>
        <Comp/>
        <Comp/>
        <Comp/>
        <Comp/>
      </ScrollView>
    
like image 39
Pritish Vaidya Avatar answered Oct 13 '22 19:10

Pritish Vaidya