ScrollView不会水平使用scrollTo()移动

时间:2018-05-22 15:28:01

标签: reactjs react-native

我有以下水平scrollView,我想滚动到X位置。

 <ScrollView horizontal={true} ref={ref => this.scrollView = ref} showsHorizontalScrollIndicator={false}>

                    <TouchableOpacity onPress={() => {this.onPressDate(I18n.t("month1"))}} style={styles.month}>
                      <View style={{alignSelf: 'center' }}>
                        <Text style={[commonStyle.normalItemBold,{color:this.state.underlineMonth1?'#000':'#9B9B9B'}]}>{I18n.t("month1")}</Text>
                        <View  style={[styles.rowSep, {opacity: this.state.underlineMonth1?100:0} ]}/>
                      </View>
                    </TouchableOpacity>

                    <TouchableOpacity onPress={() => {this.onPressDate(I18n.t("month2"))}} style={styles.month}>
                      <View   style={{alignSelf: 'center' }}>
                        <Text style={[commonStyle.normalItemBold,{color:this.state.underlineMonth2?'#000':'#9B9B9B'}]}>{I18n.t("month2")}</Text>
                        <View  style={[styles.rowSep, {opacity: this.state.underlineMonth2?100:0} ]}/>
                      </View>
                    </TouchableOpacity>

                    <TouchableOpacity onPress={() => {this.onPressDate(I18n.t("month3"))}} style={styles.month}>
                      <View   style={{alignSelf: 'center' }}>
                        <Text style={[commonStyle.normalItemBold,{color:this.state.underlineMonth3?'#000':'#9B9B9B'}]}>{I18n.t("month3")}</Text>
                        <View  style={[styles.rowSep, {opacity: this.state.underlineMonth3?100:0} ]}/>
                      </View>
                    </TouchableOpacity>

                    <TouchableOpacity onPress={() => {this.onPressDate(I18n.t("month4"))}} style={styles.month}>
                      <View   style={{alignSelf: 'center' }}>
                        <Text style={[commonStyle.normalItemBold,{color:this.state.underlineMonth4?'#000':'#9B9B9B'}]}>{I18n.t("month4")}</Text>
                        <View  style={[styles.rowSep, {opacity: this.state.underlineMonth4?100:0} ]}/>
                      </View>
                    </TouchableOpacity>

                    <TouchableOpacity onPress={() => {this.onPressDate(I18n.t("month5"))}} style={styles.month}>
                      <View   style={{alignSelf: 'center' }}>
                        <Text style={[commonStyle.normalItemBold,{color:this.state.underlineMonth5?'#000':'#9B9B9B'}]}>{I18n.t("month5")}</Text>
                        <View  style={[styles.rowSep, {opacity: this.state.underlineMonth5?100:0} ]}/>
                      </View>
                    </TouchableOpacity>

                  </ScrollView>

我在ComponentDidMount()中使用了scrollTo但它没有工作,并且scrollView没有提出移动的位置。这是代码:

componentDidMount=()=>{

   this.scrollView.scrollTo({  X:0, Y:100 });
}

你能帮我解决问题吗?

1 个答案:

答案 0 :(得分:1)

使用scrollTo({x: 0, y: 0, animated: true})方法可以滚动到某个位置。

只需为水平滚动设置x:值,或为竖直设置y:值。

在你的情况下应该是:

scrollTo({x: 100, y: 0, animated: true})
相关问题