saa*_*adq 4 flexbox react-native react-native-flexbox react-native-flatlist
我正在尝试创建一个FlatList周围有一定间距的水平线。我能够paddingLeft在列表上用正确的开头间距,但是paddingRight在列表上似乎没有任何空格(如果我一直滚动到末尾,则最后一项紧靠边框按下)。
这是一个小吃,您可以运行并现场尝试:https : //snack.expo.io/@saadq/aW50cm
这是我的代码:
import * as React from 'react';
import { Text, View, FlatList, StyleSheet } from 'react-native';
const data = [{ key: 1 }, { key: 2 }, { key: 3 }, { key: 4 }];
class App extends React.Component {
render() {
return (
<View style={styles.container}>
<FlatList
style={styles.flatList}
horizontal
data={data}
renderItem={() => (
<View style={styles.box}>
<Text>Box</Text>
</View>
)}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
flatList: {
marginTop: 100,
paddingLeft: 15,
paddingRight: 15, // THIS DOESN'T SEEM TO BE WORKING
// marginRight: 15 I can't use marginRight because it cuts off the box with whitespace
},
box: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: 50,
width: 100,
borderWidth: 1,
borderColor: 'black',
paddingHorizontal: 15,
marginRight: 15,
},
});
export default App;
Run Code Online (Sandbox Code Playgroud)
使用marginRight代替paddingRight似乎确实提供了预期的间距结果,但是这会导致另一个问题,即始终存在空白并在滚动时将其切断。任何帮助,将不胜感激!
小智 9
您可以使用“ListFooterComponent”。它是 Flatlist 的一个道具,并作为 Flatlist 的最后一个组件。因此,您可以将宽度为 15 的空视图传递给它,以使右边距起作用。尝试这个:
<FlatList
style={styles.flatList}
horizontal
data={data}
renderItem={() => (
<View style={styles.box}>
<Text>Box</Text>
</View>
)}
ListFooterComponent={<View style={{width:15}}></View>}
Run Code Online (Sandbox Code Playgroud)
重要的代码行是这样的:
ListFooterComponent={<View style={{width:15}}></View>
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用 contentInsets 属性调整 FlatList、SectionList、ScrollView 的 contentInsets。https://facebook.github.io/react-native/docs/scrollview#contentinset
例如
<FlatList
data={...}
renderItem={...}
horizontal={true}
contentInset={{ right: 20, top: 0, left: 0, bottom: 0 }}
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2545 次 |
| 最近记录: |