Expo – 嵌套水平Flatlist动态高度

huangapple go评论67阅读模式
英文:

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}
/>

我尝试计算替代项列表(水平滚动)中每个项目的高度,并在水平索引更改时为列表设置动态高度。但这导致错误:

Expo – 嵌套水平Flatlist动态高度

英文:

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.

Working vertical list

Not working horizontal list

WorkoutExerciseList (Vertical scroll flatlist)

  const renderItem = useCallback(
    ({
      item,
      drag,
      isActive,
    }: RenderItemParams&lt;WorkoutExercise | WorkoutExercise[]&gt;) =&gt; {
      if (Array.isArray(item) &amp;&amp; item.length &gt;= 2) {
        return (
          &lt;ReplacementExercisesList
            items={item}
            drag={drag}
            displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
          /&gt;
        );
      } else if (Array.isArray(item)) {
        return (
          &lt;WorkoutExercisesListItem
            {...item[0]}
            drag={drag}
            displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
          /&gt;
        );
      } else {
        return (
          &lt;WorkoutExercisesListItem
            {...item}
            drag={drag}
            displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
          /&gt;
        );
      }
    },
    []
  );

 &lt;DraggableFlatList
      contentContainerStyle={{
        // flexGrow: 1,
        // flex: 1,
        paddingBottom: 160,
      }}
      keyboardShouldPersistTaps=&quot;handled&quot;
      // style={{ flex: 1 }}
      data={workoutExercises}
      onDragEnd={onDragEnd}
      renderItem={renderItem}
      keyExtractor={(item) =&gt; {
        if (Array.isArray(item)) {
          return item[0].id;
        } else {
          return item.id;
        }
      }}
    /&gt;

ReplacementExerciseList - Horizontal scroll

 &lt;Animated.FlatList
        data={items}
        ref={ref}
        // contentContainerStyle={{ flexGrow: 1 }}
        renderItem={({ item, index }) =&gt; (
          &lt;WorkoutExercisesListItem
            drag={drag}
            {...item}
            index={index}
            displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
            isReplacementExercise={true}
            computeHeight={computeListHeight}
          /&gt;
        )}
        scrollEnabled={false}
        // onViewableItemsChanged={handleVieweableItemsChanged}
        scrollEventThrottle={32}
        keyExtractor={(item) =&gt; item.id}
        horizontal={true}
        initialScrollIndex={activeIndex}
        showsHorizontalScrollIndicator={false}
        bounces={false}
        pagingEnabled={true}
      /&gt;

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:
Expo – 嵌套水平Flatlist动态高度

答案1

得分: 1

你可以使用 minHeightflexGrow: 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) =&gt; {
    console.log(&quot;itemIndex&quot;, itemIndex);
    LayoutAnimation.easeInEaseOut();
    setActiveIndex(itemIndex);
  }, []);

 &lt;WorkoutExercisesListItem
        {...items[activeIndex]}
        displaySetFinishedHeaderIcon={displaySetFinishedHeaderIcon}
        isReplacementExercise={true}
      /&gt;

onSelectReplacementExercise is called by my switch menu.

huangapple
  • 本文由 发表于 2023年6月6日 02:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定