【问题标题】:Scroll to react-native scrollview dynamic child滚动到 react-native scrollview 动态子项
【发布时间】:2020-01-08 15:39:43
【问题描述】:

我创建了一个呈现动态表单元素的滚动视图。我可以为滚动视图的每个动态子项创建“ref”。但我面临的问题是我想滚动到表单中存在验证错误的确切子项。但是我找不到每个子项相对于滚动视图的实际位置,因此我可以滚动到该元素。

我尝试将 onLayout 方法添加到子组件,但它在加载时只返回一次位置,然后值保持不变,即使在滚动之后也是如此。

我的滚动视图

<ScrollView style={{marginBottom: 55}} ref={(ref) => { this.formScrollList = ref; }}>
  {this.state.formElements.map((element, i) => {
    return (
      <TextboxInput quesNo={element.key + 1} required={element.required} error={this.state.errors[element.question_id]} quesType={element.type} quesId={element.question_id} placeholder={element.placeholder} onChangeHandler={this.onChangeHandler} label={element.label} defaultValue={this.state.answers[element.question_id]} showRequired={this.showRequired} index={element.index}/>
    )
  })}
</ScrollView>

我的 TextboxInput 孩子

const TextboxInput = (props) => (
    <View style={styles.inputContainer} ref={setRef}>
        <Text style={styles.text}>{props.label || "Textbox"}{props.showRequired(props.required)}</Text>
        <TextInput style={[styles.textInput, props.error === "empty" ? {borderWidth: 1, borderColor: "red"} : null]} {...props} onChangeText={props.onChangeHandler.bind(this, props.quesId, props.quesType, props.index)} placeholder={props.placeholder} underlineColorAndroid="transparent" defaultValue={props.defaultValue}/>
    </View>
)

我想滚动到表单中存在验证错误的确切子项。

【问题讨论】:

  • 有可用于此目的的库。可能对你有用。试试这个react-native-scroll-into-view。可能重复question
  • 感谢您让我了解图书馆,它帮助解决了我的问题。这与您提到的问题类似,但我尝试了该解决方案中可用的所有方法,但都没有奏效。

标签: react-native scrollview positioning ref


【解决方案1】:

使用scrollview组件的scrollTo方法

【讨论】:

  • 问题不是scrollTo,而是scrollTo哪个位置
猜你喜欢
  • 2022-12-22
  • 2018-09-04
  • 2019-07-16
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
相关资源
最近更新 更多