【发布时间】:2017-10-02 05:55:41
【问题描述】:
我需要在 ScrollView 中更改一行的状态(在 ListView 中相同),但使用组件的 setState 会使滚动将其位置重置为顶部。
<ScrollView
{...this.panResponder.panHandlers}
bounces={false}
onScroll={() => {
this.scrollIsScrolling = true;
}}
onTouchEnd={() => {
this.scrollIsScrolling = false;
}}
onMomentumScrollEnd={() => {
this.scrollIsScrolling = false;
}}
scrollEnabled={this.state.scrollEnabled}
removeClippedSubviews={false}
enableEmptySections
style={styles.listView}
>
{this.props.settings.map((setting, i) => {
return (
<IntegrationSlideupCell
key={i}
title={setting.title}
selected={this.state.selectedSetting === i}
selectedGradient={this.props.integration.selectedGradient}
onPress={() => {
this.setState({ selectedSetting: i });
}}
/>
);
})}
</ScrollView>
UPD:删除自定义 panHandlers、onScroll、onTouchEnd、onMomentumScrollEnd 处理程序和其他道具没有区别。导致它设置状态的唯一原因。
【问题讨论】:
-
您是否尝试在滚动视图上保存内容大小 onContentSizeChange?
-
不,我没有。但是即使大小发生了变化,iOS 滚动视图(本机视图)也会保持位置。所以我也有同样的期待。不过我会试试的,谢谢你的建议。
-
为什么不引入
scrollToIndex()功能? -
目前通过在使用 setState 之前存储偏移量来修复。但请继续调查。
标签: ios react-native scrollview react-native-ios setstate