admin管理员组文章数量:1356294
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
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
Share Improve this question asked Apr 5, 2020 at 10:34 ntx2ntx2 311 silver badge6 bronze badges 2- Is this answer what you want ? stackoverflow./a/43512934/13107433 – 高鵬翔 Commented Apr 6, 2020 at 1:22
- I tried. Unfortunately, that didn't help. – ntx2 Commented Apr 6, 2020 at 6:47
1 Answer
Reset to default 10You 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. 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.
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)
docs
But the several begins item seems can't go to middle because of there are no data before. Or you can use react-native-infinite-looping-scroll to let other data at bottom connect back to first. Maybe can achieve this problem.
本文标签: javascriptReact NativeThe selected item is always in the middle of the FlatlistStack Overflow
版权声明:本文标题:javascript - React Native - The selected item is always in the middle of the Flatlist - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743949514a2567027.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论