Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to end of FlatList after displaying the keyboard

I have a FlatList inside a KeyboardAvoidingView. When the keyboard is displayed I would like to scroll to the end of the FlatList.

I am listening for the 'keyboardDidShow' event which does get fired, but it may be fired too early as the FlatList is not scrolled to the end after calling scrollToEnd.

I have looked into the onLayout event of KeyboardAvoidingView however just setting the onLayout event to trigger a function seems to stop the KeyboardAvoidingView from adjusting it's size when the Keyboard is shown.

<KeyboardAvoidingView behavior='padding' style={{ flex: 1}} onLayout={this._scrollEnd}> 

Code:

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>     );   } } 
like image 437
Andrew Arthur Avatar asked May 17 '17 01:05

Andrew Arthur


People also ask

How do you keep the scroll position with FlatList?

In Flatlist set scrollsToTop={false} this will retain position when you navigate back from detail screen. I am using Expo version 25.

How do I find the last item in FlatList?

You can use FlatList's ListFooterComponent prop to render some JSX or a component at the end of the list. If you need to know that you are at the end of the list within renderItem , you can check the index in the metadata provided to renderItem against the length of data .

How do I scroll to specific index in FlatList React Native?

We would use Ref. scrollToIndex() inbuilt function of FlatList here. To use this function first we have to make a reference of our FlatList component then we can call this function.

How do you scroll horizontally in FlatList React Native?

To add a horizontal FlatList in React Native, we just set the horizontal prop of the FlatList to true . to add horizontal to FlatList . As a result, we can scroll through the FlatList items horizontally.


1 Answers

I'm making a chat component and I want about the same things. Did it like this:

<FlatList    ref={ref => this.flatList = ref}    onContentSizeChange={() => this.flatList.scrollToEnd({animated: true})}    onLayout={() => this.flatList.scrollToEnd({animated: true})}    ... /> 

Keyboard popping up triggers a layout, so that's fixed. New chat messages arriving trigger content changes, so it also scrolls to the bottom (which is what I wanted for my chat window)

like image 72
Anthony De Smet Avatar answered Oct 10 '22 06:10

Anthony De Smet