单击链接后滚动到顶部 反应本机.



我有一个问题,我没有想法,如何解决。在我的反应原生项目中,我在底部显示数据列表和几个链接。单击这些链接中的任何一个后,它会导航到同一页面,并为单击的链接获取新数据,但问题是页面永远不会滚动到顶部,其位置保持不变或有时位于中间。

类文章链接:

export default class ArticleLink extends React.Component {
constructor(props) {
super(props);
this.scrollView = null;
this.state = {
isLoading: true,
data: null,
isError: false,
}
}
render() {
const { params } = this.props.navigation.state;
if(params!=null) {
getArticleLinks(params.nid).then(data => {
this.setState({
isLoading: false,
data: data
})
}, error => {
Alert.alert("Error", "Something happend, please try again")
}
)
}

<View style={{ flex: 1 }}>
<ScrollView>
<Header style={{ backgroundColor: 'black' }}>
<Left>
<Button transparent>
<Icon name='arrow-left' size={20} color="white" onPress={() => this.props.navigation.goBack()}/>
</Button>
</Left>
<Body><Image  style={{ marginTop: 20,backgroundColor: 'white' }} source={IMAGE.ICON_MENU2} style={{ height:35,width:125 }}/></Body>
<Right>
</Right>
</Header>
<View style={{ marginLeft: 15,flex: 1,fontFamily:'Tageblatt Picto', flexDirection: 'column',  justifyContent: 'center',alignItems: 'flex-start' }}>
--------------
------------
----------
<List  style={{marginBottom:50}}
dataArray={links}
renderItem={({item})=>{
return (
<ListItem style={{marginLeft: 0}} noBorder>
<TouchableOpacity onPress={() => this.props.navigation.navigate('ArticleLink',item)} style={{flexDirection:'row'}} activeOpacity={0.5}>
<Text  numberOfLines={3} style={{fontSize: 16 }} numberOfLines={2}>{`u25A0 ${item.title}`}</Text>
</TouchableOpacity>
</ListItem>
)
}} />

您可以通过向ScrollView添加引用来滚动到顶部

<ScrollView ref={(ref) => { this.scrollView = ref; }}>

当您按下链接时,您应该致电

this.scrollView.scrollTo(0)

最新更新