如何解决滚动到FlatList中的某个项目-没有确切的确切宽度
我的应用程序中有一个水平FlatList
FlatList中有很多项目
每个项目都是文本,没有确切的宽度或字符长度
所以,没有固定宽度
在这种情况下如何滚动到某个元素
scrollToIndex和scrollToOffset取决于给出我没有的宽度
我打算使用scrollToItem
每个文档,它需要道具prop
'item'(对象)-滚动到的项目。必需。
这个物品的概率是多少?
文档中不清楚,也没有在线示例
解决方法
尝试这种方式
let data = [ {key: 0,text: “A”},{key: 0,text: “B”} ]
<FlatList
// Do something when animation ended
onMomentumScrollEnd={(e) => this.onScrollEnd(e)}
showsHorizontalScrollIndicator={false}
data={this.state.data}
keyExtractor={(item) => item.key}
getItemLayout={(data,index) => (
// Max 5 items visibles at once
{length: Dimensions.get('window').width / 5,offset: Dimensions.get('window').width / 5 * index,index}
)}
horizontal
// Here is the magic : snap to the center of an item
snapToAlignment={'center'}
// Defines here the interval between to item (basically the width of an item with margins)
snapToInterval={Dimensions.get('window').width / 5}
renderItem={ ({item,index}) =>
<TouchableOpacity
onPress={() => this.scrollToIndex(/* scroll to that item */)} <!— index ->
style={styles.cell}>
<Text>{item.text}</Text>
</TouchableOpacity>
}
/>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。