简体   繁体   English

React Native - 被选中的项目总是在 Flatlist 的中间

[英]React Native - The selected item is always in the middle of the Flatlist

I want to make sure that the selected item is always in the middle of the flatlist.我想确保所选项目始终位于平面列表的中间。 The middle item is selected.中间项目被选中。 The style of the chosen one is different than the others.所选择的风格与其他人不同。

 <FlatList data={items}
                                      style={styles.listStyle}
                                      ref={(ref) => {
                                          this.flatListRef = ref;
                                      }}
                                      snapToAlignment={'center'}
                                      horizontal

                                      onPress={this.onPressButton}
                                      showsHorizontalScrollIndicator={false}
                                      renderItem={({item, index}) => (
                                          <TouchableWithoutFeedback style={{justifyContent: 'center'}}
                                                                    onPress={this.onPressButton}>
                                              <View style={{justifyContent: 'center'}}>
                                                  <View   style={styles.containerView}>
                                                      <View
                                                          style={[styles.circlesBack, (this.state.selectedId === index) ? styles.circles : styles.circlesBack]}>
                                                          {this.state.selectedId === index ?
                                                              <FontAwesome size={24} name={item.icon} color="white"/> :
                                                              <FontAwesome size={24} name={item.icon} color="#BEBEBE"/>}
                                                      </View>
                                                      {this.state.selectedId === index ? <Text
                                                              style={[styles.itemText, (this.state.selectedId === index) ? styles.itemText : styles.itemTextBack]}>{item.title}</Text> :
                                                          <Text> </Text>}

                                                  </View>
                                              </View>
                                          </TouchableWithoutFeedback>
                                      )}
                                      keyExtractor={item => item.id}/>

this is what it looks like now这就是现在的样子在此处输入图像描述

it should look like this它应该看起来像这样在此处输入图像描述

How to do it?怎么做?

Thank you谢谢

You should use like this:你应该这样使用:

this.flatListRef.scrollToIndex({animated: true, index: 30, viewPosition:0.5})

Valid params keys are:有效的参数键是:

'animated' (boolean) - Whether the list should do an animation while scrolling. 'animated' (boolean) - 滚动时列表是否应该执行 animation。 Defaults to true.默认为真。

'viewPosition' (number) - A value of 0 places the item specified by index at the top, 1 at the bottom, and 0.5 centered in the middle. 'viewPosition' (number) - 值 0 将 index 指定的项目放在顶部,1 放在底部,0.5 放在中间。

Here is someone's code for example , you can instead the line 例如,这是某人的代码,您可以改为该行

this.flatListRef.scrollToIndex({animated: true, index: 30,viewPosition:0.5});

of

this.flatListRef.scrollToIndex({animated: true, index: randomIndex});

then you can see it works(at right choose android or ios to run will be better)然后你可以看到它工作(在右边选择 android 或 ios 运行会更好)

docs文档

But the several begins item seems can't go to middle because of there are no data before.但是由于之前没有数据,这几个开始项似乎不能 go 到中间。 Or you can use react-native-infinite-looping-scroll to let other data at bottom connect back to first.或者您可以使用react-native-infinite-looping-scroll让底部的其他数据连接回第一个。 Maybe can achieve this problem.也许可以解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM