React Native Snap Carousel - 第一项

Rya*_*113 6 react-native react-native-snap-carousel

我正在尝试让我的 React Snap Carousel 加载到所需的索引上。目前,当轮播加载时,它会加载第一个索引,而不是我用 FirstItem 告诉它的索引。当我刷新屏幕或保存在代码编辑器中时,轮播会捕捉到我所指的第一个项目(索引)。下面是我的代码。我还看到了很多关于此的卡片,它们指向https://github.com/archriss/react-native-snap-carousel/blob/master/doc/KNOWN_ISSUES.md#unreliable-first-item。尽管我一生都不知道如何让它正常工作。我很感激任何帮助。谢谢。

  <Carousel
    layout={'default'}
    ref={c => {
    this._carousel = c;
    }}
    useScrollView={true}
    data={this.getDaysArray() ? this.getDaysArray() : []}
    renderItem={this.renderItem.bind(this)}
    sliderWidth={Platform.OS === 'ios' ? 375 : 392}
    itemWidth={Platform.OS === 'ios' ? 310 : 332}
    firstItem={22}
    initialScrollIndex={23}
    onLayout={() => {
      this._carousel.snapToItem(22);
      }}
  />
Run Code Online (Sandbox Code Playgroud)

car*_*eld 1

我遇到了同样的问题,由于某种原因,当我从未在其中放入空列表时,它就起作用了。当您始终将数据与N元素放在 where时,它​​会起作用firstItem < N。所以这样的事情对我有用。

if (this.getDaysArray().length===0){
    return (<View> 
              <Text>Loading screen or what ever</Text>
            <View/>)
}

return  (
<Carousel
    layout={'default'}
    ref={c => {
    this._carousel = c;
    }}
    useScrollView={true}
    data={this.getDaysArray() ? this.getDaysArray() : []}
    renderItem={this.renderItem.bind(this)}
    sliderWidth={Platform.OS === 'ios' ? 375 : 392}
    itemWidth={Platform.OS === 'ios' ? 310 : 332}
    firstItem={22}
    initialScrollIndex={23}
    onLayout={() => {
      this._carousel.snapToItem(22);
      }}
  />

)
Run Code Online (Sandbox Code Playgroud)