gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:07:47 30 4
gpt4 key购买 nike

我有一个带有 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com