我使用react本机的FlatList来显示项目列表,并检查哪些项目目前是可查看的。在我的项目中,有一个项目标记为mostUsed,如果该项目不可查看,则在顶部显示一个链接,用户可以单击该链接并使用scrollToIndex滚动到该项目。scrollToIndex在不设置numColumns的情况下工作得很好,当我设置numColumns={2}时,会得到scrollToIndex out of range: 9 vs 4错误。
setScrollIndex = () => {
if (this.state.scrollIndex !== 0) {
return;
}
for (let i = 0; i < this.state.items.length; i++) {
const items = this.state.items;
if (items[i] && items[i].mostUsed) {
this.setState({
scrollIndex: i,
});
}
}
};
// scroll to index function
scrollToIndex = () => {
this.flatListRef.scrollToIndex({
animated: true,
index: this.state.scrollIndex,
});
};
<FlatList
data={this.state.items}
numColumns={2}
keyExtractor={(item, index) => index.toString()}
ref={ref => {
this.flatListRef = ref;
}}
showsVerticalScrollIndicator={false}
onViewableItemsChanged={this.handleViewableItemsChanged}
viewabilityConfig={this.viewabilityConfig}
renderItem={({ item, index }) => (
<Card
title={item.title}
description={item.description}
mostUsed={item.mostUsed}
style={{ width: item.width }}
/>
)}
/>发布于 2019-04-01 13:45:20
看起来,如果FlatList的scrollToIndex高于1,则numColumns将改变其查看索引的方式。
将其命名为scrollToRowIndex可能更正确,因为在多列情况下,它不适用于项索引。
对于你的案子,这件事在世博会上对我起了作用:
scrollToIndex = () => {
this.flatListRef.scrollToIndex({
animated: true,
index: Math.floor(this.state.scrollIndex / numColumns),
});
};https://stackoverflow.com/questions/55456023
复制相似问题