错误的填充 | 在 SectionList 内部嵌套使用 FlatList

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

Incorrect Padding | Nested FlatList Inside SectionList

问题

我有一个SectionList,其中第一和第二节是水平滚动的。我通过在这些部分内嵌套一个FlatList来实现这一点。然而,将水平填充添加到FlatList,使其从左边开始16px,也会从右边剪切16px。

我尝试将horizontalPadding: 16更改为paddingLeft: 16paddingStart: 16,但结果相同。

如下图所示,是预期与实际情况的示例

错误的填充 | 在 SectionList 内部嵌套使用 FlatList

代码

  <SectionList
    sections={sections}
    renderSectionHeader={({ section }) => {
      const type = section.type;
      switch (type) {
        case "one":
          return (
            <FlatList
              style={{
                paddingHorizontal: 16,
                marginBottom: 16,
              }}
              data={section.data}
              horizontal
              renderItem={({ item }) => <Card1 item={item} />}
              ItemSeparatorComponent={() => (
                <View style={{ width: 8 }}></View>
              )}
              showsHorizontalScrollIndicator={false}
            />
          );
        case "two":
          return (
            <FlatList
              style={{ flex: 1, paddingLeft: 16, marginBottom: 24 }}
              data={section.data}
              horizontal
              renderItem={({ item }) => (
                <Card2 item={item} onPress={offerDetails} />
              )}
              ItemSeparatorComponent={() => (
                <View style={{ width: 16 }}></View>
              )}
              showsHorizontalScrollIndicator={false}
            />
          );
        case "three":
          return (
            <Text
              style={{
                fontSize: 21,
                fontWeight: "bold",
                paddingHorizontal: 16,
                marginBottom: 12,
              }}
            >
              {section.title}
            </Text>
          );
      }
    }}
    renderItem={({ item, section }) => {
      if (section.horizontal) {
        return null;
      }
      return <Card3 item={item} onPress={offerDetails} />
    }}
    stickySectionHeadersEnabled={false}
    showsVerticalScrollIndicator={false}
  />
英文:

I have a SectionList where I have the first and second sections as horizontally scrollable. I've done this by nesting a FlatList inside these sections. However, adding horizontal padding to the FlatList so it starts 16px from the left it also cuts off 16px from the right.

I have tried changing horizontalPadding: 16 to paddingLeft: 16 & paddingStart: 16 however this has the same outcome.

Illustration below or expected vs actual

错误的填充 | 在 SectionList 内部嵌套使用 FlatList

Code

  <SectionList
    sections={sections}
    renderSectionHeader={({ section }) => {
      const type = section.type;
      switch (type) {
        case "one":
          return (
            <FlatList
              style={{
                paddingHorizontal: 16,
                marginBottom: 16,
              }}
              data={section.data}
              horizontal
              renderItem={({ item }) => <Card1 item={item} />}
              ItemSeparatorComponent={() => (
                <View style={{ width: 8 }}></View>
              )}
              showsHorizontalScrollIndicator={false}
            />
          );
        case "two":
          return (
            <FlatList
              style={{ flex: 1, paddingLeft: 16, marginBottom: 24 }}
              data={section.data}
              horizontal
              renderItem={({ item }) => (
                <Card2 item={item} onPress={offerDetails} />
              )}
              ItemSeparatorComponent={() => (
                <View style={{ width: 16 }}></View>
              )}
              showsHorizontalScrollIndicator={false}
            />
          );
        case "three":
          return (
            <Text
              style={{
                fontSize: 21,
                fontWeight: "bold",
                paddingHorizontal: 16,
                marginBottom: 12,
              }}
            >
              {section.title}
            </Text>
          );
      }
    }}
    renderItem={({ item, section }) => {
      if (section.horizontal) {
        return null;
      }
      return <Card3 item={item} onPress={offerDetails} />;
    }}
    stickySectionHeadersEnabled={false}
    showsVerticalScrollIndicator={false}
  />

答案1

得分: 1

当使用FlatList组件时,可以通过使用contentContainerStyle属性来对滚动视图的内容容器应用样式。该属性允许您指定将应用于包装所有子视图的容器的样式。

英文:

When utilizing the FlatList component, it is possible to apply styles to the scroll view content container by making use of the contentContainerStyles property. This property allows you to specify styles that will be applied to the container that wraps all of the child views.

huangapple
  • 本文由 发表于 2023年7月3日 18:51:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604046.html
匿名

发表评论

匿名网友

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

确定