Javascript 在ScrollView中自动滚动
我正在创建聊天应用程序,并希望在每次收到新消息时在Javascript 在ScrollView中自动滚动,javascript,react-native,Javascript,React Native,我正在创建聊天应用程序,并希望在每次收到新消息时在ScrollView中自动滚动到底部 这是我的密码: <ScrollView> <FlatList data={this.state.dataSource} renderItem={({ item }) => <SenderChatRow data={item} />} keyExtractor={(item, index) => index} /> {this.
ScrollView
中自动滚动到底部
这是我的密码:
<ScrollView>
<FlatList
data={this.state.dataSource}
renderItem={({ item }) => <SenderChatRow data={item} />}
keyExtractor={(item, index) => index}
/>
{this._bookingRequestMessage()}
</ScrollView>
}
keyExtractor={(项,索引)=>index}
/>
{this.\u bookingRequestMessage()}
从React Native 0.41及更高版本,您可以使用:
<ScrollView
ref={ref => this.scrollView = ref}
onContentSizeChange={(contentWidth, contentHeight)=>{
this.scrollView.scrollToEnd({animated: true});
}}>
</ScrollView>
谢谢你的更新
用于scrollview
将这些行添加到滚动视图中
this.flatListRef=ref}
contentContainerStyle={{flex:1}}
onContentSizeChange={(宽度、高度)=>this.flatListRef.scrollToEnd({animated:false})}
>
谢谢,但现在当我打开键盘时,scrollview不会滚动到底部\删除此项后工作正常。keyboardDidHideListener.remove();来自组件willunmount()
componentDidMount() {
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow',
this._keyboardDidShow.bind(this));
}
componentWillUnmount() {
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
}
_keyboardDidShow() {
this.scrollView.scrollToEnd({ animated: false })
}