FlatList Dynamic Height Sizing

半世苍凉 提交于 2019-12-21 07:06:53

问题


I have an AutoComplete Box which gives me a list of autocomplete items. I display the items in FlatList, I also have a border around the FlatList. My code is given below: -

render(){
return (
  <View>
    <TextInput
       clearButtonMode="while-editing"
       onChangeText={this.onChangeText}
       value={this.state.searchText}
       onSubmitEditing={this.onTextSubmitted}
       placeholder="Find..." />
       {this.state.data.length > 0 &&
        <FlatList
           style={styles.list}
           keyboardShouldPersistTaps="handled"
           data={this.state.data}
           ItemSeparatorComponent={this.renderSeparator}
           keyExtractor={item => item.properties.id}
           renderItem={this.renderItem} />});
}

const styles = StyleSheet.create({
list: {
    borderWidth: 16,
    borderColor: colors.searchBorder,
  },
});

How can I increase/decrease the size of the FlatList with the number of list items, (I think the border is the reason behind this error).


回答1:


Add flexGrow: 0 to your list style.




回答2:


I had the same problem and the solutions above not work in my case.

The solution, for me, was to wrap the FlatList into a ScrollView like this :

<ScrollView>
  <FlatList
      data={this.state.listItem}
      renderItem={this.renderItem}
  />
</ScrollView>

The dynamic height will be managed by the ScollView parent.

See the documentation here.




回答3:


The border is just an indicator of boundaries of your current FlatList component. If you want the list to grow and shrink try adding flex: 1 to your list style property.

const styles = StyleSheet.create({
  list: {
    borderWidth: 16,
    borderColor: colors.searchBorder,
    flex: 1
  },
})


来源:https://stackoverflow.com/questions/46057598/flatlist-dynamic-height-sizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!