【发布时间】:2017-10-16 06:35:47
【问题描述】:
我在 KeyboardAvoidingView 中有一个 FlatList。显示键盘时,我想滚动到 FlatList 的末尾。
我正在监听 'keyboardDidShow' 事件,它确实被触发了,但它可能被触发得太早,因为在调用 scrollToEnd 后 FlatList 没有滚动到末尾。
我已经查看了 KeyboardAvoidingView 的 onLayout 事件,但是仅将 onLayout 事件设置为触发函数似乎会阻止 KeyboardAvoidingView 在显示键盘时调整其大小。
<KeyboardAvoidingView behavior='padding' style={{ flex: 1}} onLayout={this._scrollEnd}>
代码:
import React from 'react';
import {Image, Linking, Platform, ScrollView, StyleSheet, Text, TouchableOpacity, View, Button, Alert, FlatList, TextInput, KeyboardAvoidingView, Keyboard} from 'react-native';
import { MonoText } from '../components/StyledText';
export default class HomeScreen extends React.Component {
constructor() {
super();
this.state = {
messages: getMessages()
};
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._scrollEnd);
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidHide', this._scrollEnd);
}
_scrollEnd = (evt) => {
this.refs.flatList1.scrollToEnd();
}
render() {
return (
<KeyboardAvoidingView behavior='padding' style={{ flex: 1}} >
<FlatList
style={{ flex:1}}
ref="flatList1"
data={this.state.messages}
renderItem={({ item }) => <Text>{item.text}</Text>}
/>
</KeyboardAvoidingView>
);
}
}
【问题讨论】:
-
您是否尝试添加
getItemLayout属性?看起来它修复了它:facebook.github.io/react-native/releases/0.44/docs/…。或者你的物品高度不是静态的? -
你在哪个平台?赢,操作系统,林?
-
这段代码在我的电脑上运行。您能否更详细地说明发生了什么?卷轴永远不会发生?还是内容被剪掉了?
-
您发布的内容对我来说就像是一种魅力。感觉你不应该在构造函数中添加监听器,而是在 componentWillMount 中,然后在 componentWillUnmount 中再次删除它们。
标签: react-native react-native-flatlist