javascript - RefreshControl with ScrollView 每次刷新都会向下移动屏幕内容

标签 javascript react-native

我有一个带有 refreshControl 属性的 ScrollView 的屏幕。 结果是,当我刷新屏幕时,屏幕的全部内容都向下移动了几个位置。如果我再次刷新,它会继续下降,依此类推。

enter image description here enter image description here

此屏幕截图是在刷新前和刷新后截取的。如您所见,刷新后屏幕顶部添加了一些空间。为什么?

这是我的渲染函数中的内容:

render() {
  if(this.state.isLoading || this.state.user === null) {
   // IF I PUT RETURN NULL HERE IT WORKS FINE,AND THE CONTENT DOESNT GO DOWN
    return(<ScrollView contentContainerStyle={{alignItems: "center", flex: 1, justifyContent: 'center'}}>
      <Spinner isVisible={true} size={100} type={'Pulse'} color={'#013773'}/>
      <DropdownAlert ref={ref => this.dropDownAlertRef = ref} /> 
    </ScrollView>);
  }
  else {
    return (
      <ScrollView
      refreshControl={
        <RefreshControl
          refreshing={this.state.isLoading}
          onRefresh={this._onRefresh.bind(this)}
        />
      }>
        <View style={styles.whiteContainer}>
          <View style={{backgroundColor: colors.white,paddingVertical: 10, borderStyle: 'solid', borderColor: colors.black}}>
              <Text style={{fontStyle: 'italic', fontWeight: 'bold', fontSize:20, marginLeft:20, color: colors.grey}}>
                Mis donaciones
              </Text>
          </View>
          {this.state.myDonations.length > 0 ? 
          <Carousel
            ref={(c) => { this._carousel = c; }}
            data={this.state.myDonations}
            renderItem={this._renderDonation}
            sliderWidth={SLIDER_WIDTH}
            itemWidth={ITEM_WIDTH2}
            inactiveSlideShift={0}
            onSnapToItem={(index) => this.setState({ index })}
            scrollInterpolator={scrollInterpolator}
            slideInterpolatedStyle={animatedStyles}
            useScrollView={true}
            />: 
            <Text style={{fontStyle: 'italic', alignSelf: 'center', fontWeight: 'bold', fontSize:20, marginTop:20, marginBottom: 40, color: colors.grey}}>
            ¡Nada para mostrar!
          </Text>}
        </View>
        {this.state.user.category === "Voluntario" ? 
        <View style={styles.whiteContainer2}>
        <View style={{backgroundColor: colors.white,paddingVertical:10, borderStyle: 'solid', borderColor: colors.black}}>
            <Text style={{fontStyle: 'italic', fontWeight: 'bold', fontSize:20, marginLeft:20, color: colors.grey}}>
              Mis entregas
            </Text>
        </View>
        {this.state.myDeliveries.length > 0 ? 
        <Carousel
        ref={(c) => { this._carousel = c; }}
        data={this.state.myDeliveries}
        renderItem={this._renderDelivery}
        sliderWidth={SLIDER_WIDTH}
        itemWidth={ITEM_WIDTH2}
        inactiveSlideShift={0}
        onSnapToItem={(index) => this.setState({ index })}
        scrollInterpolator={scrollInterpolator}
        slideInterpolatedStyle={animatedStyles}
        useScrollView={true}
      /> : <Text style={{fontStyle: 'italic', alignSelf: 'center', fontWeight: 'bold', fontSize:20, marginTop:20, marginBottom: 40, color: colors.grey}}>
      ¡Nada para mostrar!
    </Text>}
      </View>
        : null}
        <View style={styles.whiteContainer2}>
          <View style={{backgroundColor: colors.white, paddingVertical:10, borderStyle: 'solid', borderColor: colors.black}}>
              <Text style={{fontStyle: 'italic', fontWeight: 'bold', fontSize:20, marginLeft:20, color: colors.grey}}>
                Pedidos
              </Text>
          </View>
          {this.state.requests.length > 0 ? 
          <Carousel
              ref={(c) => { this._carousel = c; }}
              data={this.state.requests}
              renderItem={this._renderRequest}
              sliderWidth={SLIDER_WIDTH}
              itemWidth={ITEM_WIDTH}
              inactiveSlideShift={0}
              onSnapToItem={(index) => this.setState({ index })}
              scrollInterpolator={scrollInterpolator}
              slideInterpolatedStyle={animatedStyles}
              useScrollView={true}
            /> : <Text style={{fontStyle: 'italic', alignSelf: 'center', fontWeight: 'bold', fontSize:20, marginTop:20, marginBottom: 40, color: colors.grey}}>
            ¡Nada para mostrar!
          </Text>}
        </View>
        <DropdownAlert ref={ref => this.dropDownAlertRef = ref} /> 
      </ScrollView>
    );
  }
}

_onRefresh 看起来像这样:

_onRefresh = () => {   
  this.setState({
    user: null,
    isLoading: true
  });
}

当 componentDidUpdate 中的方法完成加载时,稍后将 isLoading 设置为 false。

我正在从“react-native”导入 RefreshControl 和 ScrollView:“~0.61.4”。

更新:正如您在我的渲染方法中看到的那样,我得到了一个 if-else 语句。事实证明,如果我将 return null 而不是我的微调器放在第一个 block 中,问题就解决了。为什么?

最佳答案

好的,我发现了问题。 如您所见,我的渲染方法中有一个条件 if-else 语句。 事实证明,我还必须将 RefreshControl 添加到语句第一个 block 的 ScrollView。

render() {
  if(this.state.isLoading || this.state.user === null) {
    return(
    <ScrollView contentContainerStyle={{alignItems: "center", flex: 1, justifyContent: 'center'}}
    refreshControl={
      <RefreshControl
        refreshing={false}
        onRefresh={this._onRefresh.bind(this)}
      />
    }>
      ...
    </ScrollView>);
  }
  else {
    return (
      <ScrollView
      refreshControl={
        <RefreshControl
          refreshing={this.state.isLoading}
          onRefresh={this._onRefresh.bind(this)}
        />
      }>
        ...
      </ScrollView>
    );
  }
}

关于javascript - RefreshControl with ScrollView 每次刷新都会向下移动屏幕内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64398682/

相关文章:

javascript - 类型错误 : Class extends value undefined is not a constructor or null

javascript - 忽略本地时区的 Angular 日期选择器

javascript - 用js设置不透明度或关闭模态窗口

javascript - 如何将其他屏幕打开到 React Native 应用程序中?

react-native - 运行 pod install 时,React native 0.64.0 与 CocoaPods 1.10.0 不兼容

react-native - react native 如何检测左侧或右侧被触摸

Javascript揭示模块模式的缺点

javascript - 用什么代替返回 bool 值

react-native - wix react 原生导航 v2 |如何从侧边抽屉组件将新屏幕推送到当前屏幕

javascript - 如何控制 React Native 的 <TextInput> 的 defaultValue?