Scrolling issues with FlatList when rows are variable height

后端 未结 4 2130
时光说笑
时光说笑 2021-02-01 08:11

I\'m using a FlatList where each row can be of different height (and may contain a mix of both text and zero or more images from a remote server).

I cannot

4条回答
  •  眼角桃花
    2021-02-01 08:49

    I did not find any way to use getItemLayout when the rows have variable heights , So you can not use initialScrollIndex .

    But I have a solution that may be a bit slow: You can use scrollToIndex , but when your item is rendered . So you need initialNumToRender . You have to wait for the item to be rendered and after use scrollToIndex so you can not use scrollToIndex in componentDidMount .

    The only solution that comes to my mind is using scrollToIndex in onViewableItemsChanged . Take note of the example below : In this example, we want to go to item this.props.index as soon as this component is run

    constructor(props){
    
            this.goToIndex = true;
    
        }
    
    render() {
            return (
    
                     {this.myFlatList = component;}}
                        data={data}
                        renderItem={({item})=>this._renderItem(item)}
                        keyExtractor={(item,index)=>index.toString()}
                        initialNumToRender={this.props.index+1}
                        onViewableItemsChanged={({ viewableItems }) => {
                            if (this.goToIndex){
                                this.goToIndex = false;
                                setTimeout(() => { this.myFlatList.scrollToIndex({index:this.props.index}); }, 10);
                            }
                        }}
                    />
    
            );
        }
    

提交回复
热议问题