oijafoijf asnjksdjn
oijafoijf asnjksdjn

Reputation: 1225

ReactNative,How to get position FlatList Item in ScrollView

I would like to scroll the parent ScrollView to that itemposition when I click Item in FlatList.

Since the actual data is flexible in length, can not calculate the height statically.

<ScrollView>
  <View style={{height:300}}>
    <Text>Test</Text>
  </View>
  <FlatList
    scrollEnabled={false}
    data={[1,2,3,4,5,6,7,8,9,10]}
    renderItem={renderItem}
  />
</ScrollView>

ScrollToIndex in a FlatList can not be used because scrollEnabled is false. I want to use ScrollTo of the parent ScrollView.

But onLayout is used forrenderItem, y is 0. So I do not know the exact position.

How can I get the value I want?

Upvotes: 2

Views: 2494

Answers (1)

Anurag Chutani
Anurag Chutani

Reputation: 595

First of all, Don't use FlatList inside ScrollView, use ListHeaderComponent prop in FlatList

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

Upvotes: 1

Related Questions