6

I know that we can get the offset with

render() {
   return <FlatList
            onScroll={this.handleScroll}
          />
}

handleScroll = (event) => {
//get offset by using   
//event.nativeEvent.contentOffset 
}

But I'm not sure how to set it

MendyK
  • 1,643
  • 1
  • 17
  • 30

3 Answers3

7

Unfortunately contentOffset didn't work for me. I ended up using contentContainerStyle that I found in the ScrollView documentation and it works great.

<FlatList 
  contentContainerStyle={{ paddingTop: 340 }} 
  data={data}
/>
needsleep
  • 2,685
  • 10
  • 16
3

contentOffset accepts an object of type {x: 0, y: 0}:

In your case and if the list is vertical scrolling you should only set y

<FlatList
  onScroll={this.handleScroll}
  contentOffset = {{x: 0, y: your_value}}
/>

I haven't test this code but some users complain that setting the value will not work so they set the contentOffset value programmatically.

event.nativeEvent.contentOffset = { x: your_value }
Sarantis Tofas
  • 5,097
  • 1
  • 23
  • 36
-2

You may use ScrollToOffset(params)

https://facebook.github.io/react-native/docs/flatlist#scrolltooffset

or use ScrollTo(params)

https://facebook.github.io/react-native/docs/scrollview#scrollto

user1047504
  • 578
  • 7
  • 14