英文:
Expo - Nested horizontal Flatlist dynamic height
问题
以下是您要翻译的内容:
我目前正在开发一个包含练习的列表。一个新功能是某个练习可以有替代项。
所有练习以垂直方式显示在一个 FlatList 中。
练习的替代项应该水平显示。这部分工作正常。
问题:
每个练习可以列出不同数量的集。如下所示。当用户水平滚动时,嵌套的 FlatList 的高度没有相应地改变。
WorkoutExerciseList(垂直滚动 FlatList)
const renderItem = useCallback(
({
item,
drag,
isActive,
}: RenderItemParams<WorkoutExercise | WorkoutExercise[]>) => {
if (Array.isArray(item) && item.length >= 2) {
return (
<ReplacementExercisesList
items={item}
drag={drag}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
/>
);
} else if (Array.isArray(item)) {
return (
<WorkoutExercisesListItem
{...item[0]}
drag={drag}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
/>
);
} else {
return (
<WorkoutExercisesListItem
{...item}
drag={drag}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
/>
);
}
},
[]
);
<DraggableFlatList
contentContainerStyle={{
// flexGrow: 1,
// flex: 1,
paddingBottom: 160,
}}
keyboardShouldPersistTaps="handled"
// style={{ flex: 1 }}
data={workoutExercises}
onDragEnd={onDragEnd}
renderItem={renderItem}
keyExtractor={(item) => {
if (Array.isArray(item)) {
return item[0].id;
} else {
return item.id;
}
}}
/>
ReplacementExerciseList - 水平滚动
<Animated.FlatList
data={items}
ref={ref}
// contentContainerStyle={{ flexGrow: 1 }}
renderItem={({ item, index }) => (
<WorkoutExercisesListItem
drag={drag}
{...item}
index={index}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
isReplacementExercise={true}
computeHeight={computeListHeight}
/>
)}
scrollEnabled={false}
// onViewableItemsChanged={handleVieweableItemsChanged}
scrollEventThrottle={32}
keyExtractor={(item) => item.id}
horizontal={true}
initialScrollIndex={activeIndex}
showsHorizontalScrollIndicator={false}
bounces={false}
pagingEnabled={true}
/>
我尝试计算替代项列表(水平滚动)中每个项目的高度,并在水平索引更改时为列表设置动态高度。但这导致错误:
英文:
I'm currently developing a list with exercises. A new feature should be that a certain exercise can have replacements.
All exercises are displayed in a flatList vertically.
The replacements of an exercise should be displayed horizontal. That works fine.
Issue:
Each exercise can have a different amount of sets that are listed. As you can see below. When the user is scrolling horizontally the height of the nested FlatList doesn't change accordingly.
WorkoutExerciseList (Vertical scroll flatlist)
const renderItem = useCallback(
({
item,
drag,
isActive,
}: RenderItemParams<WorkoutExercise | WorkoutExercise[]>) => {
if (Array.isArray(item) && item.length >= 2) {
return (
<ReplacementExercisesList
items={item}
drag={drag}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
/>
);
} else if (Array.isArray(item)) {
return (
<WorkoutExercisesListItem
{...item[0]}
drag={drag}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
/>
);
} else {
return (
<WorkoutExercisesListItem
{...item}
drag={drag}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
/>
);
}
},
[]
);
<DraggableFlatList
contentContainerStyle={{
// flexGrow: 1,
// flex: 1,
paddingBottom: 160,
}}
keyboardShouldPersistTaps="handled"
// style={{ flex: 1 }}
data={workoutExercises}
onDragEnd={onDragEnd}
renderItem={renderItem}
keyExtractor={(item) => {
if (Array.isArray(item)) {
return item[0].id;
} else {
return item.id;
}
}}
/>
ReplacementExerciseList - Horizontal scroll
<Animated.FlatList
data={items}
ref={ref}
// contentContainerStyle={{ flexGrow: 1 }}
renderItem={({ item, index }) => (
<WorkoutExercisesListItem
drag={drag}
{...item}
index={index}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
isReplacementExercise={true}
computeHeight={computeListHeight}
/>
)}
scrollEnabled={false}
// onViewableItemsChanged={handleVieweableItemsChanged}
scrollEventThrottle={32}
keyExtractor={(item) => item.id}
horizontal={true}
initialScrollIndex={activeIndex}
showsHorizontalScrollIndicator={false}
bounces={false}
pagingEnabled={true}
/>
I tried to calculate the height of each item in the ReplacementExerciseList (Horizontal) and set a dynamic height for the list, whenever the active horizontal index changes. But this ends up in an Error:
答案1
得分: 1
你可以使用 minHeight 和 flexGrow: 0(在这里找到)来处理动态高度,但我建议使用 RecyclerView,因为它具有更好的动态高度选项,以及在动态大小方面更好的性能。
英文:
You can use minHeight and flexGrow: 0 (here you can find) to handle dynamic height but I would recommend using RecyclerView as this have better options for dynamic height plus better performance with dynamic sizings.
答案2
得分: 0
I removed the second horizontal flatList. Instead, I render only ONE list item. When the user is switching to another item, I will set a new index. The item always renders the data of the selected index:
const onSelectReplacementExercise = useCallback((itemIndex: any) => {
console.log("itemIndex", itemIndex);
LayoutAnimation.easeInEaseOut();
setActiveIndex(itemIndex);
}, []);
<WorkoutExercisesListItem
{...items[activeIndex]}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
isReplacementExercise={true}
/>
onSelectReplacementExercise is called by my switch menu.
英文:
I removed the second horizontal flatList. Instead i render only ONE list item. When the user is switching to another item i will set a new index. The item always renders the data of the selected index:
const onSelectReplacementExercise = useCallback((itemIndex: any) => {
console.log("itemIndex", itemIndex);
LayoutAnimation.easeInEaseOut();
setActiveIndex(itemIndex);
}, []);
<WorkoutExercisesListItem
{...items[activeIndex]}
displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
isReplacementExercise={true}
/>
onSelectReplacementExercise is called by my switch menu.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论