【问题标题】:Spacing before and after a horizontal FlatList (React Native)水平 FlatList 前后的间距(React Native)
【发布时间】:2019-04-12 19:23:41
【问题描述】:

我正在尝试创建一个水平的FlatList,它周围有一些间距。我能够使用列表中的paddingLeft 正确地获得开始间距,但列表中的paddingRight 似乎没有在其后放置任何空格(如果我一直滚动到最后,则按下最后一项正对着边界)。

这是一个 Snack,您可以运行并现场试用: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;

使用marginRight 而不是paddingRight 似乎确实给出了预期的间距结果,但是它会导致一个不同的问题,即空白始终存在并在滚动时切断项目。任何帮助将不胜感激!

【问题讨论】:

    标签: react-native flexbox react-native-flatlist react-native-flexbox


    【解决方案1】:

    我使用这种方法来解决问题:

     ItemSeparatorComponent={() => <View style={{ width: 35 }} />}
    

    【讨论】:

      【解决方案2】:
      <FlatList
        // extraData={this.props.x}
        contentContainerStyle={{paddingBottom: 10, paddingTop: 8}}
        data={contacts}
        removeClippedSubviews={false}
        keyExtractor={(item, index) => index.toString()}
        // onRefresh={false}
        renderItem={({item}) => (
          <View
            style={{
              flex: 1,
              flexDirection: 'row',
              justifyContent: 'space-between',
              // margin: 3,
              marginVertical: 5,
              marginHorizontal: 4,
              // borderWidth: 1,
              // borderColor: '#d6d7da',
              borderRadius: 7,
              // padding: 1,
              // borderRightWidth: 1, borderRightColor: '#d6d7da'
            }}>
           <Text>HOLA</Text>
          </View>
        )}
      />
      

      【讨论】:

        【解决方案3】:
        contentContainerStyle={{paddingBottom:xxx}} 
        

        【讨论】:

          【解决方案4】:

          您可以使用 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 }}
          
          />
          

          【讨论】:

            【解决方案5】:

            似乎我能够通过在 FlatList 上使用 contentContainerStyle 属性而不是直接传递 style 属性来修复它。

            【讨论】:

            • 如果你正在寻找文档中的道具,你不会在 FlatList 上找到它。 FlatList 继承 ScrollView 的 props 所以你会找到它 there
            【解决方案6】:

            您可以使用“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>}
            

            重要的代码行是这样的:

            ListFooterComponent={<View style={{width:15}}></View>
            

            【讨论】:

            • 有趣的方法。我能够通过使用不同的道具 (contentContainerStyle) 来解决它,但我很感激你的回应,赞成!
            • 谢谢.. 我必须承认你的方法可能更干净。我的更像是一种解决方法。但如果它的工作,那么它的好:)
            • 我试过了 - 这是个好主意,但是在重新呈现列表时视图会丢失
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-08-07
            • 2017-12-31
            • 2017-07-03
            • 1970-01-01
            • 2013-02-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多