使用scrollToEnd
方法来滚动到列表底部,同时使用setTimeout
来延迟滚动,以便在FlatList成功渲染所有项之后再滚动到指定索引处。代码示例:
import { FlatList } from 'react-native';
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.flatListRef = React.createRef();
}
componentDidMount() {
setTimeout(() => {
this.flatListRef.current.scrollToEnd({ animated: false });
this.flatListRef.current.scrollToIndex({
index: 5,
animated: true,
viewPosition: 0.5,
});
}, 100);
}
renderItem = ({ item }) => {
return {item} ;
};
render() {
const { data } = this.props;
return (
index.toString()}
/>
);
}
}