【发布时间】:2022-01-25 04:00:31
【问题描述】:
我有一个水平的Flatlist,其中的项目可以有不同的高度。如何获取所有元素或特定可见元素的高度,并根据元素的高度更改Flatlist 的高度?
我在Snack 上创建了一个与我的代码接近的代码。在示例中,data 中的高度用于示例,在我的代码中我不知道这个高度。
非常感谢您的帮助!
import React from 'react';
import { View, StyleSheet, FlatList, Text, Dimensions } from 'react-native';
const {width} = Dimensions.get('window');
const Item = ({item}) => {
return (
<View style={{width, height: item.height, backgroundColor: 'yellow'}}>
<Text>{item.type}</Text>
<Text>{item.text}</Text>
</View>
);
};
export default function App() {
const data = [
{
height: 100, //FOR EXAMPLE
type: 'row 1',
text: 'row 1'
},
{
height: 200, //FOR EXAMPLE
type: 'row 2',
text: 'row 2'
},
{
height: 150, //FOR EXAMPLE
type: 'row 3',
text: 'row 3'
},
];
return (
<View>
<FlatList
style={{backgroundColor: 'red'}}
data={data}
keyExtractor={item => item.id}
renderItem={({item}) => (
<Item item={item} />
)
}
horizontal
pagingEnabled
/>
</View>
);
}
【问题讨论】:
标签: react-native react-native-flatlist flatlist