英文:
Incorrect Padding | Nested FlatList Inside SectionList
问题
我有一个SectionList,其中第一和第二节是水平滚动的。我通过在这些部分内嵌套一个FlatList来实现这一点。然而,将水平填充添加到FlatList,使其从左边开始16px,也会从右边剪切16px。
我尝试将horizontalPadding: 16
更改为paddingLeft: 16
和paddingStart: 16
,但结果相同。
如下图所示,是预期与实际情况的示例
代码
<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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论