scrollToIndex를 사용하려면 getItemLayout을 사용해야합니다. getItemLayout을 사용할 의사가 없으면 사용하는 것이 중요하지 않습니다.
class ScrollToExample extends Component {
getItemLayout = (data, index) => (
{ length: 50, offset: 50 * index, index }
)
scrollToIndex =() => {
let randomIndex = Math.floor(Math.random(Date.now()) *
this.props.data.length);
this.flatListRef.scrollToIndex({animated: true, index:
randomIndex});
}
scrollToItem =() => {
let randomIndex = Math.floor(Math.random(Date.now()) *
this.props.data.length);
this.flatListRef.scrollToIndex({animated: true, index: "" +
randomIndex});
}
render() {
return (
<FlatList
style={{ flex: 1 }}
ref={(ref) => { this.flatListRef = ref; }}
keyExtractor={item => item}
getItemLayout={this.getItemLayout}
initialScrollIndex={50}
initialNumToRender={2}
renderItem={({ item, index}) => (
<View style={{...style, backgroundColor: this.getColor(index)}}>
<Text>{item}</Text>
</View>
)}
{...this.props}
/>
);
} }
이 도움이
https://gist.github.com/joshyhargreaves/b8eb67d24ce58a6d8bffb469f7eeaf39
희망 : 여기에 반응-기본 문서에서 가져온 예입니다!
글쎄,이리스트에서 getItemLayout을 사용할 수 없다면 어떻게 도움이 될지 모르겠다./ –
@JakubSzczepanik 어쨌든 나를 지키지 말라. 왜 당신은 편평한 목록에 그것을 사용할 수 없습니까? – ShaneG
@JakubSzczepanik 개별 메모 항목의 크기에 관계없이 해당 색인으로 계속 스크롤합니다. getItemLayout에서 해당 데이터를 전달할 수도 있습니다. 따라서 각 항목의 크기를 추적하려는 경우 해당 데이터를 평면 목록에 저장하고 렌더링 할 때 getItemLayout으로 전달할 수 있습니다 – ShaneG