Horizontal scrollview snapping react native

前端 未结 4 1977
臣服心动
臣服心动 2021-01-31 09:19

Hi I am trying to achieve scrollview snap to center like below gif link

Check This Gif

But unable to do so. Following is my react nat

4条回答
  •  情歌与酒
    2021-01-31 10:07

    If you don't want to use snapToInterval because of the misalignment for long lists you can use snapToOffsets which is more precise.

    for example:

    const { width } = Dimensions.get('window');
    this.IMAGE_WIDTH = width * (1 / 2.5)
    this.image_margin = 5
    this.nishhar = width - ((this.IMAGE_WIDTH + this.image_margin) * 2 + this.image_margin * 2)
    
    dataNum = [1, 2, 3, 4, 5, 6]
    
    return ( {
                    return (
                        
                        )
                }}
                keyExtractor={this.keyGenerator}
                horizontal={true}
                snapToAlignment={"start"}
                snapToOffsets={[...Array(dataNum.length)].map((x, i) => (i * (this.IMAGE_WIDTH + 2 * this.image_margin) - (this.nishhar * 0.5)))}
                decelerationRate={"fast"}
                pagingEnabled
            />)
    
    

    or you can also checkout this example: https://snack.expo.io/SyWXFqDAB

提交回复
热议问题